Search code examples
asp.net-mvc-3dependency-injectioninversion-of-controlstructuremapcontroller-factory

MVC3 / Structure Map 2.6.2 DI custom controller factory problem


I'm having problems with passing non-parameterless classes as models to a view in a controller.

I recently moved from Structure Map 2.5.3 to 2.6.2. Everything worked fine in 2.5.3 nad it doesn't work anymore in 2.6.2. Here is my Custom Controller factory:

public class StructureMapControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        try
        {
            return ObjectFactory.GetInstance(controllerType) as Controller;
        }
        catch (StructureMapException)
        {
            Debug.WriteLine(ObjectFactory.WhatDoIHave());
            throw;
        }
    }
}

And wiring it:

ControllerBuilder.Current.SetControllerFactory(typeof(StructureMapControllerFactory));

My Custom model binder throws an exception: http://screencast.com/t/xZDNAAmM

What could be a problem?


Solution

  • I don't think this has anything to do with your DI container. When instance is null the call to your modelbinder is made and probably it tries to create a new instance of modelType which is impossible as that doesn't have a parameterless constructor.

    I think you just added a constructor parameter to the constructor of your modelType