gpt4 book ai didi

c# - 将自定义依赖项解析器与 Unity 和 Web API Controller 一起使用时出错

转载 作者:太空狗 更新时间:2023-10-29 18:16:01 25 4
gpt4 key购买 nike

我正在使用以下类作为依赖项解析器。从 http://www.asp.net/web-api/overview/advanced/dependency-injection 获得引用

   public class UnityWebAPIResolver : IDependencyResolver
{
protected IUnityContainer container;

public UnityWebAPIResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}

public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType); **// This line throws error**
}
catch (ResolutionFailedException)
{
return null;
}
}

public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType); **// This line throws error**
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}

public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityWebAPIResolver(child);
}

public void Dispose()
{
container.Dispose();
}
}

在路由配置后的 WebApiConfig 类中,我正在像这样配置依赖解析器

config.DependencyResolver = new UnityWebAPIResolver(UnityConfig.GetContainer());

问题是我遇到了几个这样的错误..

InvalidOperationException - The type IHostBufferPolicySelector does not have an accessible constructor.
InvalidOperationException - The type ModelMetadataProvider does not have an accessible constructor.
InvalidOperationException - The type ITraceManager does not have an accessible constructor.
InvalidOperationException - The type ITraceWriter does not have an accessible constructor.
InvalidOperationException - The type IHttpControllerSelector does not have an accessible constructor.
InvalidOperationException - The type IAssembliesResolver does not have an accessible constructor.
InvalidOperationException - The type IHttpControllerTypeResolver does not have an accessible constructor.
InvalidOperationException - The type IHttpActionSelector does not have an accessible constructor.
InvalidOperationException - The type IActionValueBinder does not have an accessible constructor.
InvalidOperationException - The type IContentNegotiator does not have an accessible constructor.
InvalidOperationException - The type IHttpControllerActivator does not have an accessible constructor.
InvalidOperationException - The type IBodyModelValidator does not have an accessible constructor.

即使我尝试在我的 global.asax 中做这样的事情,我也会遇到同样的错误。

GlobalConfiguration.Configuration.DependencyResolver = new UnityWebAPIResolver(UnityConfig.GetContainer());

问题:我的 API Controller 中的所有依赖项似乎都已正确注入(inject),我唯一担心的是因为它无法解决上面提到的几个问题(特定于框架) 类型,是否有可能导致整个框架故障并产生随机错误?

最佳答案

没有问题,您的程序在这里按预期运行。您看到的是 Unity 的 Resolve 方法抛出的第一次机会异常。抛出异常是因为当无法解析服务时,Unity 永远不会返回 nullIDependencyResolver.GetService 但是,如果请求的服务未在依赖项解析器实现中注册,则实现应始终返回 null

如果 GetService 返回 null,MVC 将回退到框架对所请求服务的默认实现。在大多数情况下,无需在 Unity 容器中覆盖这些服务,即使需要不同的服务,您也可以轻松替换 MVC 的默认实现,而无需将其添加到 Unity 配置中。

但由于 Unity 预计会抛出此异常,因此这些方法包含 catch 子句。因此,您遇到的异常在该方法中被捕获并返回 null。

显然,启动应用程序后调试器多次停在那些方法上是非常烦人的,所以解决方案是用[DebuggerStepThrough]属性标记这些方法,以防止调试器在这些方法中停止。

关于c# - 将自定义依赖项解析器与 Unity 和 Web API Controller 一起使用时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27975276/

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