如何在服务中访问WebHost配置?

时间:2017-01-18 10:53:05

标签: c# asp.net-core .net-core

我正在配置一个简单的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);

但我无法在qwe1中找到Startup配置。这是一个构造函数:

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
}

不足为奇,但我在IHostingEnvironmentAddHostConfiguration()等扩展方法中找不到任何内容。那么将主机初始化配置注入Startup类的正确方法是什么?

0 个答案:

没有答案