不使用AutofacWebApiDependencyResolv

不使用AutofacWebApiDependencyResolv

本文介绍了MVC的WebAPI不使用AutofacWebApiDependencyResolver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个混合MVC 4应用程序,其中一些控制器控制器部分控制器 ApiController 。我还使用Autofac为DI,但现在看来,该控制器的WebAPI激活机械(对于缺乏更具体的期限)未使用Autofac解析器( AutofacWebApiDependencyResolver ),这导致当我提出反对我的API控制器之一的请求被抛出异常。这里的错误:

I have a mixed MVC 4 app, where some controllers are regular implementations of Controller and some controllers are implementations of ApiController. I'm also using Autofac for DI, but it appears that the WebApi controller "activator" machinery (for lack for a more specific term) is not using the Autofac resolver (AutofacWebApiDependencyResolver), which leads to an exception being thrown when I make a request against one of my api controllers. Here's the error:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
        Type 'MyApp.Areas.Api.Controllers.MyController' does not have a default constructor
    </ExceptionMessage>
    <ExceptionType>System.ArgumentException</ExceptionType>
    <StackTrace>
        at System.Linq.Expressions.Expression.New(Type type)
        at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
        at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
        at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
    </StackTrace>
</Error>

下面是我如何设置它:

DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);

我的Autofac.WebApi集成的理解是,上面的是获得的WebAPI使用Autofac解析器唯一的要求,所以可能什么呢?

My understanding of the Autofac.WebApi integrations is that the above is the only requirement for getting WebApi to use the Autofac resolver, so what might be going on?

侧面说明的:这个只有愚蠢的一部分,我能想到可能有轴承的是,我有在MVC地区的WebAPI控制器,​​它不是由 DefaultHttpControllerSelector ,所以实现了自定义一(VIS-à-可见Andrew MALKOV )。我不直觉这会对解析器任何轴承,但是,因为选择器只返回其后来在激活中使用的类型。

side note: The only goofy part of this that I can think of that might have bearing is that I have WebApi controllers in an MVC Area which isn't supported by the DefaultHttpControllerSelector, so implemented a custom one (vis-à-vis Andrew Malkov). I don't intuit that this would have any bearing on the resolver, though, because the selector only returns a type which is later used in activation.

推荐答案

要设置为Autofac你Web.API需要做两件事情:

To setup Autofac for Web.API you need to do two things:


  1. 注册Autofac依赖解析器(在A​​pp_Start):

  1. Register the Autofac dependency resolver (in the App_Start):

GlobalConfiguration.Configuration.DependencyResolver =
    new AutofacWebApiDependencyResolver(container);


  • 和在呼叫你的容器制造商注册你的API控制器:

  • And register your Api controllers in your container builder with the call:

    containerBuilder.RegisterApiControllers(Assembly.GetExecutingAssembly());
    


  • 这篇关于MVC的WebAPI不使用AutofacWebApiDependencyResolver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    07-22 11:20