我正在配置一个简单的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
}
不足为奇,但我在IHostingEnvironment
和AddHostConfiguration()
等扩展方法中找不到任何内容。那么将主机初始化配置注入Startup
类的正确方法是什么?