【发布时间】:2016-10-17 11:21:31
【问题描述】:
我的暂存服务器上有两个网站,它们都是在 IIS 中运行的 ASP.NET Core 网站。我已将环境变量 ASPNETCORE_ENVIRONMENT 设置为 Staging 机器范围。这适用于其中一个站点,但另一个站点忽略该变量并改为在生产模式下运行。我必须将托管环境配置到web.config 文件中才能在暂存模式下运行。
为什么一个站点不考虑环境变量?
在我的两个Startup(IHostingEnvironment env) 构造函数中,我都使用了环境变量:
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddJsonFile("logging.json")
.AddEnvironmentVariables(); // <---
Configuration = builder.Build();
}
【问题讨论】:
-
只是为了仔细检查-您确定,ASPNETCORE_ENVIRONMENT=Staging 在第二个实例上,但是 env.EnvironmentName 为它返回“Production”并且不使用来自 appsettings.Staging.json 的值?对了,你不会忘记发布 appsettings.Staging.json 文件吗?
-
我不在这两个网站上使用
appsettings.Staging.json。 ASPNETCORE_ENVIRONMENT 在机器范围内设置。第二个站点日志的启动日志记录:“环境:生产”。 -
你能检查一下 Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") 返回什么吗?
-
[09:20:33 INF] ASPNETCORE_ENVIRONMENT = '' Hosting environment: Production通过 IIS 启动时,[08:56:07 INF] ASPNETCORE_ENVIRONMENT = 'Staging' Hosting environment: Staging直接在控制台上独立启动时。
标签: asp.net-core environment-variables