.Net Core + Autofac

.Net Core + Autofac,第1张

Enviroment:

.Net(5.0)

Autofac(6.2.0)

Autofac.Configuration(6.0.0)

Autofac.Extensions.DependencyInjection(7.1.0)

1. first intall follow packages

 2. use autofac in ‘Program.cs‘ file to replace original IOC of .net core

 3. add ConfigureContainer methord  in startup,

 4. add autofac.json file  

{ "defaultAssembly": "AutofacService", "components": [ { "type": "AutofacService.RemoteLogger, AutofacService", "services": [ { "type": "AutofacIService.ISimpleLogger,AutofacIService" } ], "injectProperties": true } ] }

5. register configuration module in startup 

public void ConfigureContainer(ContainerBuilder builder) { //builder.RegisterType<SimpleLogger>().As<ISimpleLogger>().InstancePerLifetimeScope(); ////register a generic type like Respository<> //builder.RegisterGeneric(typeof(DatabaseExtension<>)).As(typeof(IDatabaseExtension<>)).InstancePerLifetimeScope(); //var assemblyIService = Assembly.Load("autofac_demo.IService"); //var assemblyService = Assembly.Load("autofac_demo.Service"); //builder.RegisterAssemblyTypes(assemblyIService, assemblyService).Where(t => t.Name.EndsWith("Service")).AsImplementedInterfaces(); //load config json var config = new ConfigurationBuilder().Add(new JsonConfigurationSource() { Path = "Config/autofac.json", Optional = false, ReloadOnChange = true }); builder.RegisterModule(new ConfigurationModule(config.Build())); //builder.Build(); }

6. test successful

 

.Net Core + Autofac

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/1006977.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存