gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 00:35:17 26 4
gpt4 key购买 nike

为什么 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 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

最佳答案

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com