ASP.NET MVC 3:IDepencyResolver正在尝试获取IControllerFactory的实现

时间:2011-02-27 16:22:02

标签: c# asp.net asp.net-mvc-3

为什么 IDependecyResolver 尝试获取 IControllerFactory 的实例,尽管我注册了 DefaultControllerFactory

的Global.asax:

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

解析器:

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);
    }
}

抛出错误:

  

StructureMap异常代码:202 No.   为。定义的默认实例   PluginFamily   System.Web.Mvc.IControllerFactory,   System.Web.Mvc,Version = 3.0.0.0,   文化=中性,   公钥= 31bf3856ad364e35

1 个答案:

答案 0 :(得分:8)

在MVC3中,DependencyResolver(它是一个服务定位器)用于尝试查找适用的类型。如果找不到类型,则它将继续查看使用遗留代码,即ControllerBuilder.Current实例。这里重要的是它通过DependencyResolver检查您的StructureMap容器​​。 MVC3要求DependencyResolver在找不到类型时返回null,框架不负责从容器中捕获任何异常。