【问题标题】:Errors while using custom dependency resolver with Unity and Web API controller将自定义依赖解析器与 Unity 和 Web API 控制器一起使用时出错
【发布时间】:2015-01-16 00:03:21
【问题描述】:

我使用以下类作为依赖解析器。参考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 控制器中的所有依赖项似乎都已正确注入,我唯一担心的是它无法解决上述几个 (特定于框架) 类型,是否有可能导致整个框架出现故障并产生随机错误?

【问题讨论】:

  • 您必须在某些时候映射自定义类型,这并不是为您神奇地完成的。你有 Unity.config 这样做吗?
  • 是的,我的所有自定义类型都已映射,并且它不会对我的自定义类型抛出任何错误...它只会对未注册到我的容器中的 MVC 框架类型抛出错误..
  • 啊,是的,我以前也遇到过这个问题。我修复了它,但我将不得不查看我的源代码以了解如何。给我几个小时,我会发布一个答案。 :)
  • 太棒了,当然 :) 并将其发布为您的答案,以便我接受。非常感谢

标签: c# asp.net-web-api unity-container


【解决方案1】:

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

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

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

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

【讨论】:

  • 感谢 Steven,提供如此详细的答案。我喜欢这样:)
猜你喜欢
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多