.net core 集成 autofac.
生活随笔
收集整理的這篇文章主要介紹了
.net core 集成 autofac.
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. Install
Install-Package Autofac Install-Package Autofac.Extensions.DependencyInjection2.Startup
2.1 增加成員
public IContainer ApplicationContainer { get; private set; }2.2 Startup.ConfigureServices
返回值改為:IServiceProvider
末尾中增加:
//******************* autofac start *********************** // Create the container builder. var autofacBuilder = new ContainerBuilder();autofacBuilder.RegisterType<TCPCollectorApplicationService>().As<ITCPCollectorApplicationService>(); autofacBuilder.Populate(services); this.ApplicationContainer = autofacBuilder.Build();return new AutofacServiceProvider(this.ApplicationContainer); //******************* autofac start ***********************3. Usage
3.1 構造注入
直接構造注入即可使用。
public TodoController(IKnowledgeApplicationService knowledgeApplicationService, ITCPCollectorApplicationService tcpCollectorApplicationService, IServiceProvider serviceProvider) {KnowledgeApplicationService = knowledgeApplicationService;TCPCollectorApplicationService = tcpCollectorApplicationService;ServiceProvider = serviceProvider; }3.2 使用ServiceProvider獲取。
var tcpSvc = ServiceProvider.GetService(typeof(ITCPCollectorApplicationService)) as ITCPCollectorApplicationService; return Ok(tcpSvc.GetAll());Ref:官方文檔: http://docs.autofac.org/en/latest/integration/aspnetcore.html#
轉載于:https://www.cnblogs.com/pengzhen/p/6912823.html
總結
以上是生活随笔為你收集整理的.net core 集成 autofac.的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript中闭包实现的私有属性
- 下一篇: jQuery从入门到忘记