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

查看:88
本文介绍了部署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等,这些文件将被加载并覆盖.

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()被称为之后 .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.

由于在评论中,有些人将其理解为设置环境的唯一方法,所以设置环境变量的方法有很多(大多数记录在

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. 环境变量(全局上,在Powershell上为Windows cmd.exe set ASPNETCORE_ENVIRONMENT=Development$Env:ASPNETCORE_ENVIRONMENT = "Development",在Linux上为export ASPNETCORE_ENVIRONMENT = Development)
  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 run -e ASPNETCORE_ENVIRONMENT=Debugging
  • 的Docker容器 在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对其进行设置(请参阅文档)
  • 可以通过环境变量设置
  • Azure App Service,可以在每个插槽中设置不同的插槽,以用于登台,开发,生产以及部署到登台,进行预热和与生产交换
  • 通过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天全站免登陆