部署asp.net核心应用时如何处理环境差异? [英] How to deal with environment differences when deploying asp.net core application?
问题描述
有没有办法在部署 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:
- 环境变量(全局,Windows cmd.exe
set ASPNETCORE_ENVIRONMENT=Development
or$Env:ASPNETCORE_ENVIRONMENT = "Development"
on powershell,export ASPNETCORE_ENVIRONMENT = Development
在 Linux 上) - 每个命令环境变量(即 linux:
ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll
) Docker 容器,即通过 docker-compose.yaml
- Environment Variable (globally, Windows cmd.exe
set ASPNETCORE_ENVIRONMENT=Development
or$Env:ASPNETCORE_ENVIRONMENT = "Development"
on powershell,export ASPNETCORE_ENVIRONMENT = Development
on linux) - Per command environment variable (i.e. linux:
ASPNETCORE_ENVIRONMENT=Production dotnet MyApp.dll
) Docker container, i.e. via docker-compose.yaml
web:
environment:
- ASPNETCORE_ENVIRONMENT=Debugging
docker run -e ASPNETCORE_ENVIRONMENT=Debugging
在 IIS 中通过 web.config.
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>
dotnet run --launch-profile Development
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屋!