本文介绍了如何在.net core 2上启动时访问IConfigurationRoot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用实体框架编写了自定义ConfigurationProvider.由于我也想使其在运行时可更新,因此我创建了 IWritableableOption .

I have written a custom ConfigurationProvider with the entity framework. Since I also want to make it updateable during runtime, I have created a IWritableableOption.

我需要在更新后刷新配置.可以通过 IConfigurationRoot.Reload .

I need to refresh the configuration after the update. This can be done via IConfigurationRoot.Reload.

但是,如何在.net core 2中获得IConfigurationRoot?

However, how can I get the IConfigurationRoot in .net core 2?

我发现,在以前的版本中,IConfigurationRoot是启动的一部分.但是,在.net core 2中,我们只有更简单的类型IConfiguration:

What I have found, is that in previous versions the IConfigurationRoot was part of startup. In .net core 2 however, we have only the simpler type 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()
})

但是我想更新启动所使用的配置.

But I want to update the configuration used by Startup.

那么如何获得Startup所使用的IConfigurationRoot注入到我的服务集合中?

So how can I get the IConfigurationRoot used by Startup to inject it into my service collection?

推荐答案

感谢.

我们可以下调IConfiguration:

public Startup(IConfiguration configuration)
{
    Configuration = (IConfigurationRoot)configuration;
}

public IConfigurationRoot Configuration { get; }

我仍然不确定这是否是预期的方式,因为IConfiguration不能对IConfigurationRoot做出任何保证.

I am still not sure, if this is the intended way, since IConfiguration does not make any guarantees about IConfigurationRoot.

这篇关于如何在.net core 2上启动时访问IConfigurationRoot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-18 19:16