【问题标题】:Autofac and ASP.NET Web API ApiControllerAutofac 和 ASP.NET Web API ApiController
【发布时间】:2012-08-30 01:14:36
【问题描述】:

我已经在 MVC 3 中使用 autofac 有一段时间了,我很喜欢它。我最近将一个项目升级到 MVC 4,除了 Web Api ApiControllers 之外,一切似乎都在工作。我收到以下异常。

An error occurred when trying to create a controller of type 'MyNamespace.Foo.CustomApiController'. Make sure that the controller has a parameterless public constructor.

在我看来,这似乎是通过 autofac 进行 DI 的问题。我是否遗漏了某些东西,或者有什么东西在进行中。我知道,MVC4 刚刚问世并且是一个测试版,所以我不抱太大希望,但我想我可能会遗漏一些东西。

【问题讨论】:

    标签: autofac asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    我已经在 NuGet 上为 MVC 4 和 Web API 的 Beta 版本发布了 Autofac 集成包。集成将为每个控制器请求(MVC 控制器或 API 控制器,具体取决于集成)创建一个 Autofac 生命周期范围。这意味着控制器及其依赖项将在每次调用结束时自动释放。这两个包可以并排安装在同一个项目中。

    MVC 4

    https://nuget.org/packages/Autofac.Mvc4

    http://alexmg.com/post/2012/03/09/Autofac-ASPNET-MVC-4-(Beta)-Integration.aspx

    网络 API

    https://nuget.org/packages/Autofac.WebApi/

    http://alexmg.com/post/2012/03/09/Autofac-ASPNET-Web-API-(Beta)-Integration.aspx

    链接现已修复。

    【讨论】:

    • 不幸的是,在输入此评论时,似乎无法同时使用 Autofac for MVC 4 RC 和 Web API RC。我收到有关在 Web API 框架程序集中找不到类型的编译器警告。我不得不使用@tugberk 提供的答案并重新实现依赖解析器来解决它。
    【解决方案2】:

    我刚刚在我的一个应用上配置了这个。有不同的方法,但我喜欢这种方法:

    Autofac and ASP.NET Web API System.Web.Http.Services.IDependencyResolver Integration

    首先我创建了一个实现System.Web.Http.Services.IDependencyResolver接口的类。

    internal class AutofacWebAPIDependencyResolver : System.Web.Http.Services.IDependencyResolver {
    
        private readonly IContainer _container;
    
        public AutofacWebAPIDependencyResolver(IContainer container) {
    
            _container = container;
        }
    
        public object GetService(Type serviceType) {
    
            return _container.IsRegistered(serviceType) ? _container.Resolve(serviceType) : null;
        }
    
        public IEnumerable<object> GetServices(Type serviceType) {
    
            Type enumerableServiceType = typeof(IEnumerable<>).MakeGenericType(serviceType);
            object instance = _container.Resolve(enumerableServiceType);
            return ((IEnumerable)instance).Cast<object>();
        }
    }
    

    我还有另一个班级持有我的注册:

    internal class AutofacWebAPI {
    
        public static void Initialize() {
            var builder = new ContainerBuilder();
            GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
                new AutofacWebAPIDependencyResolver(RegisterServices(builder))
            );
        }
    
        private static IContainer RegisterServices(ContainerBuilder builder) {
    
            builder.RegisterAssemblyTypes(typeof(MvcApplication).Assembly).PropertiesAutowired();
    
            builder.RegisterType<WordRepository>().As<IWordRepository>();
            builder.RegisterType<MeaningRepository>().As<IMeaningRepository>();
    
            return
                builder.Build();
        }
    }
    

    然后,将其初始化为Application_Start:

    protected void Application_Start() {
    
        //...
    
        AutofacWebAPI.Initialize();
    
        //...
    }
    

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-04
      • 1970-01-01
      • 2012-03-22
      • 2012-03-14
      • 2014-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多