Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 3: IDepencyResolver is trying to get an implementation of IControllerFactory

Why is the IDependecyResolver trying to get an instance of IControllerFactory although I registered the DefaultControllerFactory?

Global.asax:

ControllerBuilder.Current.SetControllerFactory(typeof(DefaultControllerFactory));
DependencyResolver.SetResolver(new StructureMapDependencyResolver());

Resolver:

public class StructureMapDependencyResolver : IDependencyResolver
{
    public static Func<Type, object> GetServiceViaDepencencyCallback = t =>
    {
        throw new NotImplementedException(
            "StructureMapDependencyResolver is not configured!");
    };

    public static Func<Type, IEnumerable<object>> GetServicesViaDepencencyCallback = t =>
    {
        throw new NotImplementedException(
            "StructureMapDependencyResolver is not configured!");
    };

    public object GetService(Type serviceType)
    {
        return GetServiceViaDepencencyCallback(serviceType);
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        return GetServicesViaDepencencyCallback(serviceType);
    }
}

Thrown error:

StructureMap Exception Code: 202 No Default Instance defined for PluginFamily System.Web.Mvc.IControllerFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35

like image 666
Rookian Avatar asked Feb 25 '23 23:02

Rookian


1 Answers

In MVC3, the DependencyResolver (which is a service locator) is used to attempt to locate an applicable type. If it cannot find a type, then it resumes looking through using the legacy code, which is the ControllerBuilder.Current instance. The important thing here is that it checks via the DependencyResolver, your StructureMap container. MVC3 requires that the DependencyResolver returns null for when it cannot find a type, it won't be the framework's responsibility to catch any exceptions from your container.

like image 121
Matthew Abbott Avatar answered Feb 27 '23 12:02

Matthew Abbott