【问题标题】:Autofac WebAPI Common Service LocatorAutofac WebAPI 通用服务定位器
【发布时间】:2015-08-12 14:38:40
【问题描述】:

所以阅读Autofac documentation 将它与 WebApi 一起使用,它说:

OWIN 集成中的一个常见错误是使用GlobalConfiguration.Configuration

我有一个BaseController,它使用IMediator,要从容器中获取一个实例,我使用GlobalConfiguration.Configuration.DependencyResolver.GetService()

如果我不使用GlobalConfiguration,我还有什么其他选项可以注入IMediator

最好不要使用构造函数注入,因为这样其他 api 控制器不需要有构造函数,因为IMediator 很少会。

我看过属性注入,但我不明白如何将它集成到 web api 场景中,我使用 builder.RegisterApiControllers 并且看起来好像在基本控制器上集成属性注入不适合这种方法.

【问题讨论】:

    标签: c# asp.net-web-api autofac


    【解决方案1】:

    builder.RegisterApiControllers() 返回IRegistrationBuilder,这意味着您可以从那里继续自定义控制器注册。 例如,如果您需要对控制器应用属性注入,您可以使用以下代码:

    builder.RegisterApiControllers().PropertiesAutowired();
    

    【讨论】:

      猜你喜欢
      • 2011-11-03
      • 1970-01-01
      • 2023-03-12
      • 2012-10-20
      • 1970-01-01
      • 2014-12-06
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      相关资源
      最近更新 更多