部署asp.net核心应用时如何处理环境差异?

How to deal with environment differences when deploying asp.net core application?

部署 ASP.NET 核心应用程序时是否有更改环境设置的方法(例如使用 debug/release 构建的配置文件转换)?

在 .NET Core 应用程序中维护多个环境设置的最佳方法是什么(对于本地、暂存和生产类似于 <appSettings file="local.config">)?

中央配置文件是 appsettings.json,您可以有多个文件,如 appsettings.Production.json 等,它们将被加载并覆盖 appsettings.json.[=31= 中的设置]

例如

        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(hostEnv.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true)
            .AddEnvironmentVariables();

要使此工作正常进行,您需要的只是用于设置环境类型的环境变量(请参阅文档 here)。

如果您将 AddEnvironmentVariables() 添加到您的配置生成器,您还可以拥有覆盖的环境变量。所以如果你有

的 appsettings.json
{
    "Data"  {
         "Default" {
              "ConnectionString" : "..."
         }
    }
}

并希望通过环境变量覆盖它,您可以设置一个名为 "Data:Default:ConnectionString" 的环境变量,它的值将覆盖 appsettings.config 和 appsettings.Production.config 中的设置(假设您的 .AddEnvironmentalVariables() 被调用 after .AddJsonFile() - 最后一次使用匹配密钥的注册获胜)环境变量的值。

您可以在官方文档中找到更多信息 here

更新

由于在评论中有些人认为这是设置环境的唯一方法,因此设置环境变量的方法有很多种(大部分都记录在 Use multiple environments in ASP.NET Core 中),最终归结为环境变量,只是在不同的范围内:

  1. 环境变量(全局,Windows cmd.exe set ASPNETCORE_ENVIRONMENT=Development or $Env:ASPNETCORE_ENVIRONMENT = "Development" on powershell,export ASPNETCORE_ENVIRONMENT = Development on linux)
  2. 每个命令环境变量(即 linux: ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll
  3. Docker 容器,即通过 docker-compose.yaml

    web:
        environment:
        - ASPNETCORE_ENVIRONMENT=Debugging
    
  4. Docker 容器通过命令行 docker run -e ASPNETCORE_ENVIRONMENT=Debugging
  5. 在 IIS 中通过 web.config.

    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" >
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    
  6. 在 IIS 上按 AppPool 设置它(参见 here
  7. 在 Linux 通过服务定义文件(参见 docs
  8. 通过环境变量的 Azure 应用服务,可以按槽设置,并且具有不同的槽用于暂存、开发、生产,即部署到暂存、进行预热和与生产交换
  9. 每次通过 dotnet run --launch-profile Development
  10. 执行

它们都是 change/set 特定范围内的环境变量(全局、本地到容器、应用程序池内部、每次执行等)。选择一个适合您需要的。

使用额外的 appsettings.*.json 文件是一个不错的选择。 * 片段可用于混合在任何区分机器、用户或部署场景的独特环境 属性 中。

但是我推荐另一种方法,而不是使用 new ConfigurationBuilder() 从头开始​​构建配置对象(如网络上的许多来源所示)。以下代码不会替换您现有的配置,但会添加到它:

    public IHostingEnvironment _environment { get; }
    public IConfiguration _configuration { get; }

    public Startup(IConfiguration configuration, IHostingEnvironment environment)
    {
        _environment = environment;

        // use the default config and add config from appsettings.COMPUTERNAME.json (if it exists)
        var builder = new ConfigurationBuilder()
            .SetBasePath(environment.ContentRootPath)
            .AddConfiguration(configuration)
            .AddJsonFile($"appsettings.{System.Environment.GetEnvironmentVariable("COMPUTERNAME")}.json", optional: true);
        _configuration = builder.Build();

    }

背景:

当您基于 dotnet new template, then your project already comes with a useful config that is automatically built through the CreateDefaultBuilder() 方法创建项目时。此默认配置结合了来自多个来源的信息:appsettings.json、appsettings.{Environment}.json、Secret Manager、环境变量和命令行参数。

如果您自己完全重建配置,您将失去所有的魔力。

提示:

上面的例子中appsettings.COMPUTERNAME.json只是一个例子。您可以根据 _environmentSystem.Environment 中的任何数据组成您自己的 json 文件名,明确区分您的不同开发和部署方案。