简体   繁体   English

如何在运行时设置 ASPNETCORE_ENVIRONMENT 变量

[英]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.

相关问题 如何为控制台应用程序设置 ASPNETCORE_ENVIRONMENT? - How to set ASPNETCORE_ENVIRONMENT for console application? 如何在 AzureDevops 中为 Dockerfile 设置环境变量(ASPNETCORE_ENVIRONMENT)? - How to set environment variables (ASPNETCORE_ENVIRONMENT) in AzureDevops for Dockerfile? 是否可以重命名 ASPNETCORE_ENVIRONMENT 变量? - Is it possible to rename ASPNETCORE_ENVIRONMENT variable? 在构建配置中设置aspnetcore_environment - Set aspnetcore_environment within build configuration 如何设置要考虑发布 ASP.NET Core 应用程序的 AS.NETCORE_ENVIRONMENT - How to set ASPNETCORE_ENVIRONMENT to be considered for publishing an ASP.NET Core application ".NET Core 控制台应用程序和 aspnetcore_environment 变量" - .NET Core console app and aspnetcore_environment variable 在T4模板中获取ASPNETCORE_ENVIRONMENT? - Get ASPNETCORE_ENVIRONMENT in T4 Template? 创建新的ASPNETCORE_ENVIRONMENT进行开发 - Create new ASPNETCORE_ENVIRONMENT for Development 将ASPNETCORE_ENVIRONMENT设置为Production时,内联页面JQuery函数不起作用 - Inline page JQuery function does not work when set ASPNETCORE_ENVIRONMENT to Production 为什么在Azure上设置ASPNETCORE_ENVIRONMENT变量时需要发布我的project.json - Why do I need to publish my project.json when setting ASPNETCORE_ENVIRONMENT variable on Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM