Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to integrate autofac with .net core

Shulou Source: shulou.com Published: 2022-06-01 09:25:52 09月26日 Update

This article will explain in detail how. Net core integrates autofac. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

1. InstallInstall-Package AutofacInstall-Package Autofac.Extensions.DependencyInjection2.Startup2.1 adds member public IContainer ApplicationContainer {get; private set;} 2.2 Startup.ConfigureServices

The returned value is changed to: IServiceProvider

Add at the end:

/ / * autofac start * *

/ / Create the container builder.

Var autofacBuilder = new ContainerBuilder (); autofacBuilder.RegisterType (). As (); autofacBuilder.Populate (services); this.ApplicationContainer = autofacBuilder.Build (); return new AutofacServiceProvider (this.ApplicationContainer)

/ / * autofac start * * 3. Usage3.1 construction injection

It can be used by direct injection.

Public TodoController (IKnowledgeApplicationService knowledgeApplicationService, ITCPCollectorApplicationService tcpCollectorApplicationService, IServiceProvider serviceProvider) {KnowledgeApplicationService = knowledgeApplicationService; TCPCollectorApplicationService = tcpCollectorApplicationService; ServiceProvider = serviceProvider;} 3.2 is obtained using ServiceProvider. Var tcpSvc = ServiceProvider.GetService (typeof (ITCPCollectorApplicationService)) as ITCPCollectorApplicationService;return Ok (tcpSvc.GetAll ()); this is the end of the article on ".net core how to integrate autofac". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Articles more good practical content article end knowledge reference help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info macOS Redmi Docker Apple