本文介绍了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值
发布评论