简体   繁体   English

Autofac WebAPI通用服务定位器

[英]Autofac WebAPI Common Service Locator

So reading Autofac documentation on using it with WebApi, it says: 因此,请阅读有关与WebApi一起使用的Autofac文档 ,它说:

A common error in OWIN integration is use of the GlobalConfiguration.Configuration OWIN集成中的一个常见错误是使用GlobalConfiguration.Configuration

I have a BaseController which uses the IMediator and to get an instance from the container I use the GlobalConfiguration.Configuration.DependencyResolver.GetService() . 我有一个BaseController ,它使用IMediator并从容器中获取实例,我使用GlobalConfiguration.Configuration.DependencyResolver.GetService()

If I don't use GlobalConfiguration what other options do I have to inject the IMediator . 如果我不使用GlobalConfiguration什么其他选择我必须注入IMediator

It's nice not to use constructor injection because then other api controllers don't need to have a constructor, and because of IMediator rarely will. 最好不要使用构造函数注入,因为那样的话,其他api控制器就不需要构造函数了,而且由于IMediator很少IMediator

I've looked at property injection but I couldn't understand how to integrate it in the web api scenario, I use the builder.RegisterApiControllers and it doesn't look as though integrating property injection on the base controller fits this approach. 我已经看过属性注入,但是我不明白如何将其集成到Web api场景中,我使用了builder.RegisterApiControllers ,看起来在基础控制器上集成属性注入并不适合这种方法。

builder.RegisterApiControllers() returns IRegistrationBuilder which means you can continue customizing Controller registration from there. builder.RegisterApiControllers()返回IRegistrationBuilder ,这意味着您可以从那里继续自定义Controller注册。 For instance if you need to apply property injection to your Controllers you could use the following code: 例如,如果您需要对控制器应用属性注入,则可以使用以下代码:

builder.RegisterApiControllers().PropertiesAutowired();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM