部署asp.net核心应用时如何处理环境差异? [英] How to deal with environment differences when deploying asp.net core application?

查看:16
本文介绍了部署asp.net核心应用时如何处理环境差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在部署 ASP.NET Core 应用程序时更改环境设置(例如使用调试/发布构建的配置文件转换)?

Is there a way to change the environment settings when deploying ASP.NET Core application (like config file transformations using debug/release build)?

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

What would be the best approach for maintaining multiple environment settings in .NET Core applications (something similar to <appSettings file="local.config"> for local, staging and production)?

推荐答案

中央配置文件是appsettings.json,你可以有多个文件,比如appsettings.Production.json 等,它将被加载并覆盖 appsettings.json 中的设置.

The central configuration file is the appsettings.json and you can have multiple files, like appsettings.Production.json etc, which will be loaded and override settings from the appsettings.json.

例如

        // 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();

您只需要设置环境类型的环境变量(参见文档 这里).

All you need to get this working is the environment variable for setting the environment type (see documentation here).

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

You can also have environment variables that override, if you add AddEnvironmentVariables() to your configuration builder. So if you have a appsettings.json of

{
    "Data"  {
         "Default" {
              "ConnectionString" : "..."
         }
    }
}

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

and want to override that via environment Variable, you'd set up an environment variable called "Data:Default:ConnectionString" and it's value will override the settings in the appsettings.config and appsettings.Production.config (assuming your .AddEnvironmentalVariables() is called after .AddJsonFile() - Last registration with the matching key wins) with the value from the environment variable.

您可以在这里的官方文档中找到更多信息.

You can find more in the official documentation here.

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

Since in the comments some understand this as the only way to set the environment, there are many ways to set environment variable (most of it is documented in Use multiple environments in ASP.NET Core), all ultimately boiling down to being an environment variable, just within a different scope:

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

  1. Environment Variable (globally, Windows cmd.exe set ASPNETCORE_ENVIRONMENT=Development or $Env:ASPNETCORE_ENVIRONMENT = "Development" on powershell, export ASPNETCORE_ENVIRONMENT = Development on linux)
  2. Per command environment variable (i.e. linux: ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll)
  3. Docker container, i.e. via docker-compose.yaml

web:
    environment:
    - ASPNETCORE_ENVIRONMENT=Debugging

  • 通过命令行的 Docker 容器 docker run -e ASPNETCORE_ENVIRONMENT=Debugging
  • 在 IIS 中通过 web.config.

  • Docker container via command line docker run -e ASPNETCORE_ENVIRONMENT=Debugging
  • in IIS via web.config.

    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" forwardWindowsAuthToken="false" stdoutLogEnabled="true" >
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    

  • 在 IIS 上按 AppPool 设置(请参阅 这里)
  • 在 Linux 上通过服务定义文件(参见 docs)
  • Azure 应用服务通过环境变量进行设置,可以为每个插槽设置不同的插槽,用于暂存、开发、生产,即部署到暂存、预热和与生产交换
  • 通过dotnet run --launch-profile Development
  • 每次执行

  • On IIS set it per AppPool (see here)
  • On Linux via service definition files (see docs)
  • Azure App Service via Environment variables, can be set per slot and having different slots for Staging, Development, Production and i.e. deploying to staging, doing warm up and swapping with Production
  • Per execution via dotnet run --launch-profile Development
  • 它们都在特定范围内更改/设置环境变量(全局、本地到容器、应用程序池内、每次执行等).选择一个适合您的需求.

    They all change/set the environment variable in a specific scope (globally, locally to a container, inside the app pool, per execution etc.). Choose one which suits your needs.

    这篇关于部署asp.net核心应用时如何处理环境差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆