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 )步骤№1中的模块已注册到 Startup.cs
2) Module from step№1 registered into Startup.cs
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModule());
}
如何访问 appsettings.json $来自
AutofacModule
的c $ c>值?我需要在 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屋!
查看全文