ASP.NET 核心忽略 ASPNET_ENV 和 Hosting:Environment
ASP.NET Core ignores ASPNET_ENV and Hosting:Environment
无论我何时何地设置ASPNET_ENV
或Hosting:Environment
,启动代码总是输入
//This method is invoked when ASPNET_ENV is 'Production'
//The allowed values are Development,Staging and Production
public void ConfigureProduction(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(minLevel: LogLevel.Warning);
Configure(app);
}
到目前为止我尝试了什么
- 在项目属性中将
Hosting:Environment
设置为 Development
- 在项目属性中将
ASPNET_ENV
设置为Development
- 在
launchSettings.json
中将Hosting:Environment
设置为Development
- 在
launchSettings.json
中将ASPNET_ENV
设置为Development
- 在调用
ConfigurationBuilder.GetEnvironmentVariables()
之前,通过 Startup
方法中的 Environment.SetEnvironmentVariable("ASPNET_ENV", "Development");
在代码中将 ASPNET_ENV
设置为 Development
顺便说一下,这是版本 1.0.0-rc2-20143
。我是不是遗漏了什么,还是只是一个错误?
如果您还没有这样做,请尝试将环境变量添加到配置实例中。
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");
builder.AddEnvironmentVariables();
var config = builder.Build();
}
作为名称更改的一部分,环境变量名称已更改为 ASPNETCORE_ENVIRONMENT
。
已在 this issue and change in this PR 中宣布。
虽然 Henk Mollema 的回答对于环境变量名称是正确的,但我仍然遇到 dotnetcore 似乎忽略环境变量 并在以下情况下使用 appsettings.json 的问题运行 从命令行。
为确保它使用正确的环境和应用设置,请尝试以下操作:
- 通过
Project Properties -> Debug -> Environment Variables -> ASPNETCORE_ENVIRONMENT -> Value
更改环境变量:更改值以匹配您的预期环境,例如 Staging
Build Solution
- 在命令行中,键入
SETX ASPNETCORE_ENVIRONMENT "YOUR-ENVIRONMENT-NAME"
,其中替换 "YOUR-ENVIRONMENT-NAME" 以匹配您在第 1 步中设置的内容,例如 Staging,并确保包含 "quotes"
- 重要 - 确保你所有的 appsettings.json 例如 appsettings.staging.json 存在于你将 运行 它的项目目录中。 (
In the case of a published solution, the appsettings.staging.json may not have been copied, so ensure it's there
)
- 在命令行中,转到您的项目目录(或已发布的目录)并通过键入
dotnet run
(或“dotnet YOURPROJECTNAME.DLL
”用于已发布的项目)运行您的项目
- 观察命令行中出现的下一行,其中指出
Hosting environment: YOURENVIRONMENTNAME
例如托管 environment:staging
Running a DotNetCore project from Visual Studio always picked the
correct appsettings based on the environment set in your project
properties, however these are the steps I followed to run dotnet core correctly from commandline
working correctly.
这对我使用 .net core 2.0 和构建网络很有效 api。请注意访问环境变量的两种不同方法。
Program.cs
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args){
if(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ") == "Production"){
return WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
} else {
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
但是在启动文件中还有另一个环境选项
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
var connectionString = "Data Source=tcp:<some-ip>,<some-port>;Initial Catalog=<some database>;Integrated Security=False;User Id=SA;Password=<some password>;MultipleActiveResultSets=True";
services.AddDbContext<myContext>(opt => opt.UseSqlServer(connectionString));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("AllowAll");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}
无论我何时何地设置ASPNET_ENV
或Hosting:Environment
,启动代码总是输入
//This method is invoked when ASPNET_ENV is 'Production'
//The allowed values are Development,Staging and Production
public void ConfigureProduction(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(minLevel: LogLevel.Warning);
Configure(app);
}
到目前为止我尝试了什么
- 在项目属性中将
Hosting:Environment
设置为Development
- 在项目属性中将
ASPNET_ENV
设置为Development
- 在
launchSettings.json
中将 - 在
launchSettings.json
中将 - 在调用
ConfigurationBuilder.GetEnvironmentVariables()
之前,通过
Hosting:Environment
设置为Development
ASPNET_ENV
设置为Development
Startup
方法中的 Environment.SetEnvironmentVariable("ASPNET_ENV", "Development");
在代码中将 ASPNET_ENV
设置为 Development
顺便说一下,这是版本 1.0.0-rc2-20143
。我是不是遗漏了什么,还是只是一个错误?
如果您还没有这样做,请尝试将环境变量添加到配置实例中。
public static void Main(string[] args)
{
var builder = new ConfigurationBuilder();
builder.AddJsonFile("appsettings.json");
builder.AddEnvironmentVariables();
var config = builder.Build();
}
作为名称更改的一部分,环境变量名称已更改为 ASPNETCORE_ENVIRONMENT
。
已在 this issue and change in this PR 中宣布。
虽然 Henk Mollema 的回答对于环境变量名称是正确的,但我仍然遇到 dotnetcore 似乎忽略环境变量 并在以下情况下使用 appsettings.json 的问题运行 从命令行。
为确保它使用正确的环境和应用设置,请尝试以下操作:
- 通过
Project Properties -> Debug -> Environment Variables -> ASPNETCORE_ENVIRONMENT -> Value
更改环境变量:更改值以匹配您的预期环境,例如 Staging Build Solution
- 在命令行中,键入
SETX ASPNETCORE_ENVIRONMENT "YOUR-ENVIRONMENT-NAME"
,其中替换 "YOUR-ENVIRONMENT-NAME" 以匹配您在第 1 步中设置的内容,例如 Staging,并确保包含 "quotes" - 重要 - 确保你所有的 appsettings.json 例如 appsettings.staging.json 存在于你将 运行 它的项目目录中。 (
In the case of a published solution, the appsettings.staging.json may not have been copied, so ensure it's there
) - 在命令行中,转到您的项目目录(或已发布的目录)并通过键入
dotnet run
(或“dotnet YOURPROJECTNAME.DLL
”用于已发布的项目)运行您的项目 - 观察命令行中出现的下一行,其中指出
Hosting environment: YOURENVIRONMENTNAME
例如托管 environment:staging
Running a DotNetCore project from Visual Studio always picked the correct appsettings based on the environment set in your project properties, however these are the steps I followed to run dotnet core correctly from commandline working correctly.
这对我使用 .net core 2.0 和构建网络很有效 api。请注意访问环境变量的两种不同方法。
Program.cs
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args){
if(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT ") == "Production"){
return WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
} else {
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
但是在启动文件中还有另一个环境选项
Startup.cs
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => options.AddPolicy("AllowAll", p => p.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()));
var connectionString = "Data Source=tcp:<some-ip>,<some-port>;Initial Catalog=<some database>;Integrated Security=False;User Id=SA;Password=<some password>;MultipleActiveResultSets=True";
services.AddDbContext<myContext>(opt => opt.UseSqlServer(connectionString));
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors("AllowAll");
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
}
}