为什么 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);
    }
}

抛出错误:

最佳答案

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

关于c# - ASP.NET MVC 3 : IDepencyResolver is trying to get an implementation of IControllerFactory,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5134313/

10-15 10:55