我用 Entity Framework 编写了一个自定义的ConfigurationProvider
。因为我还想让它在运行时可更新,所以我创建了一个 IWritableableOption
.
更新后我需要刷新配置。这可以通过 IConfigurationRoot.Reload
完成.
但是,如何在 .net core 2 中获取 IConfigurationRoot
?
我发现,在以前的版本中,IConfigurationRoot
是启动的一部分。然而,在 .net core 2 中,我们只有更简单的类型 IConfiguration
:
public Startup(IConfiguration configuration)
{
// I tried to change this to IConfigurationRoot,
// but this results in an unresolved dependency error
Configuration = configuration;
}
public IConfiguration Configuration { get; }
我也发现了,我可以使用
WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration(context, builder) => {
var configurationRoot = builder.build()
})
但我想更新 Startup 使用的配置。
那么如何获取 Startup
使用的 IConfigurationRoot
并将其注入(inject)到我的服务集合中呢?
最佳答案
感谢Dealdiane's评论。
我们可以向下转换IConfiguration
:
public Startup(IConfiguration configuration)
{
Configuration = (IConfigurationRoot)configuration;
}
public IConfigurationRoot Configuration { get; }
我仍然不确定这是否是预期的方式,因为 IConfiguration
不对 IConfigurationRoot
做出任何保证。
关于c# - 如何在 .net core 2 启动时访问 IConfigurationRoot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48939567/