C# 如何访问服务内部的WebHost配置?
我正在配置一个简单的C# 如何访问服务内部的WebHost配置?,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,我正在配置一个简单的TestServer,如下所示: var config = new ConfigurationBuilder() .AddInMemoryCollection(new Dictionary<string, string> { { "qwe1","zzzzzzzzz"} }) .Build(); var webHostBuilder = new WebHostBuilder() .UseConfiguratio
TestServer
,如下所示:
var config = new ConfigurationBuilder()
.AddInMemoryCollection(new Dictionary<string, string>
{
{ "qwe1","zzzzzzzzz"}
})
.Build();
var webHostBuilder = new WebHostBuilder()
.UseConfiguration(config)
.UseStartup<Startup>();
_server = new TestServer(webHostBuilder);
这并不奇怪,但我在
IHostingEnvironment
和扩展方法(如AddHostConfiguration()
)中没有发现任何内容。那么,将配置从主机初始化注入到Startup
类的正确方法是什么?如果要在Startup中创建新配置,必须使用env.configuration,如果要检索使用UseConfiguration的配置集。@Gusman但是这个env.configuration
在哪里,我正在寻找类似的东西,但是找不到您希望在TestServer使用的内存配置中添加配置值并在实际启动类中读取该值的原因是什么?@Ignas我需要能够关闭或重新配置测试管道的某些功能(日志记录、授权)。我不希望此配置在生产中可修改。如果要在启动中创建新配置,则必须使用env.configuration来检索使用UseConfiguration设置的配置。@Gusman但此env.configuration
在哪里,我正在寻找类似的配置,但是找不到您希望在TestServer使用的内存配置中添加配置值并在实际启动类中读取该值的原因是什么?@Ignas我需要能够关闭或重新配置测试管道的某些功能(日志记录、授权)。我不想在生产中修改此配置。
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", true)
.AddEnvironmentVariables();
_configuration = builder.Build();
var q = _configuration["qwe1"]; // q is null
}