gpt4 book ai didi

dependency-injection - 来自 Autofac 模块的 Net Core : access to appsettings. json 值

转载 作者:行者123 更新时间:2023-12-04 08:21:08 24 4
gpt4 key购买 nike

AspNet 核心应用

1)这样的Autofac模块

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
public void ConfigureContainer(ContainerBuilder builder)
{
builder.RegisterModule(new AutofacModule());
}

如何访问 appsettings.json来自 AutofacModule 的值?我需要它来在 AutofacModule 中创建我的对象并将其用于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));
}

我不知道是否是“最佳实践”方式,但它有效。

关于dependency-injection - 来自 Autofac 模块的 Net Core : access to appsettings. json 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46888812/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com