[英]How to set ASPNETCORE_ENVIRONMENT variable at runtime
This is how I set EnvironmentName
variable when publishing my application:这是我在发布应用程序时设置
EnvironmentName
变量的方式:
dotnet publish -c Release -r win-x64 /p:EnvironmentName=MyCustomValue
I got that from this link:我从这个链接得到的:
How to set ASPNETCORE_ENVIRONMENT to be considered for publishing an ASP.NET Core application 如何设置 ASPNETCORE_ENVIRONMENT 以考虑发布 ASP.NET 核心应用程序
As a result my application will try to find settings values from:因此,我的应用程序将尝试从以下位置查找设置值:
appsettings.MyCustomValue.json
file and if that file does not exist it will attempt to get it from appsettings.json
. appsettings.MyCustomValue.json
文件,如果该文件不存在,它将尝试从appsettings.json
获取它。
Anyways I will like to change the value of EnvironmentName
variable at runtime.无论如何,我想在运行时更改
EnvironmentName
变量的值。 The reason is because I only want to publish my application once and distribute it to multiple servers.原因是我只想发布我的应用程序一次并将其分发到多个服务器。 It makes no sense to have to publish the same application multiple time each with a different
EnvironmentName
variable.必须多次使用不同的
EnvironmentName
变量发布同一个应用程序是没有意义的。
This is what I have tried doing这是我尝试做的
WebApplicationBuilder builder = WebApplication.CreateBuilder();
builder.WebHost.UseEnvironment(System.IO.File.ReadAllText(/path/to/some/file...));
But that does not work.但这不起作用。
You should configure your hosting environment (Azure/AWS/Container orchestrator/whatever) to set ASPNETCORE_ENVIRONMENT=YourCustomValue
environment variable for the app.您应该配置您的托管环境(Azure/AWS/Container Orchestrator/whatever)以设置应用程序的
ASPNETCORE_ENVIRONMENT=YourCustomValue
环境变量。 Each environment should provide different values.每个环境应提供不同的值。 For instance, in AWS ECS you would do:
例如,在 AWS ECS 中,您会这样做:
{
"family": "",
"containerDefinitions": [
{
"name": "app",
"image": "app:latest",
...
"environment": [
{
"name": "ASPNETCORE_ENVIRONMENT",
"value": "YourCustomValue"
}
],
...
}
],
...
}
In docker-compose:在 docker-compose 中:
version: '3'
services:
app:
image: app:latest
environment:
- ASPNETCORE_ENVIRONMENT: 'YourCustomValue'
If you start it from a shell script:如果从 shell 脚本启动它:
ASPNETCORE_ENVIRONMENT=YourCustomValue dotnet App.dll
Sorry I did not knew I could add a Configuration to an existing configuration.抱歉,我不知道我可以将配置添加到现有配置中。 This is what I ended up doing to override the default
appsettings.MyApp.json
configuration file这就是我最终覆盖默认
appsettings.MyApp.json
配置文件所做的事情
var filePath = Path.Combine(AppContext.BaseDirectory, $"appsettings.{MyApp.InstanceId}.json");
if (File.Exists(filePath))
{
var newConfigBuilder = new ConfigurationBuilder().AddJsonFile(filePath, optional: true, reloadOnChange: true);
builder.Configuration.AddConfiguration(newConfigBuilder.Build());
}
Now if that file exists my app will first try to read values from that file and if it does not exist it will then failover to appsettings.MyApp.json
and if that file does not exist then it will failover to appsettings.json
现在,如果该文件存在,我的应用程序将首先尝试从该文件中读取值,如果它不存在,它将故障转移到
appsettings.MyApp.json
,如果该文件不存在,那么它将故障转移到appsettings.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.