Net Core:从 Autofac 模块访问 appsettings.json 值 [英] Net Core: access to appsettings.json values from Autofac Module
本文介绍了Net Core:从 Autofac 模块访问 appsettings.json 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
AspNet 核心应用
AspNet core app
1) 像这样的 Autofac 模块
1) Autofac module like that
public class AutofacModule : Module
{
protected override void Load(ContainerBuilder builder)
{
//Register my components. Need to access to appsettings.json values from here
}
}
2) step№1的模块注册到Startup.cs
2) Module from step№1 registered into Startup.cs
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModule());
}
如何从 AutofacModule
访问 appsettings.json
值?我需要它来在 AutofacModule
中创建我的对象并将其用于 DI.
How to access to appsettings.json
values from AutofacModule
? I need that for create my objects inside AutofacModule
and using it for DI.
推荐答案
需要更改步骤 №2
public void ConfigureContainer(ContainerBuilder builder)
{
//get settigns as object from config
var someSettings= Configuration.GetSection(typeof(SomeSettings).Name).Get<SomeSettings>();
//put settings into module constructor
builder.RegisterModule(new AutofacModule(someSettings));
}
我不知道是否是最佳实践"方式,但它有效.
I don't know is "best practise" way or not, but it works.
这篇关于Net Core:从 Autofac 模块访问 appsettings.json 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文