[英]Can we use a single appsettings.json file instead of maintaining multiple versions of appsettings.{environmentname}.json for different environments?
I'm using .NET 6 and I want to achieve 2 things:我正在使用 .NET 6,我想实现两件事:
appsettings.json
file instead of maintaining multiple versions of appsettings.{environmentname}.json
for different environments使用单个appsettings.json
文件而不是为不同的环境维护多个版本的appsettings.{environmentname}.json
appsettings.{environmentname}.json
file to allow (1) (Currently I'm hardcoding secrets like DB connection string, key vault configration details etc. for different environments in respective appsettings files which I want to remove and instead read the same secret values from Azure Key Vault where I'm already storing the same)从appsettings.{environmentname}.json
文件中移除硬编码以允许 (1)而是从 Azure Key Vault 中读取相同的秘密值,我已经在其中存储了相同的秘密值)How can I achieve these?我怎样才能实现这些?
Below code snippet is how I'm maintaining 2 appsettings.{environmentname}.json
files (Development & Release)
:下面的代码片段是我如何维护 2 个appsettings.{environmentname}.json
文件(Development & Release)
:
var builder = WebApplication.CreateBuilder(args);
var hostingEnvironment = builder.Environment;
if (hostingEnvironment.IsDevelopment())
{
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.Development.json", false, true)
.AddEnvironmentVariables();
}
else
{
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.Release.json", false, true)
.AddEnvironmentVariables();
}
Note: I did some research and understood that for (1)
we can probably do something like:注意:我做了一些研究,了解到对于(1)
我们可能会做类似的事情:
builder.Configuration.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable(
"ASPNETCORE_ENVIRONMENT")}.json", false, true)
.AddEnvironmentVariables();
But then, assuming the fact that the app would be automatically deployed every time leveraging CI/CD, can I set the value of AS.NETCORE_ENVIRONMENT
in the pipeline?但是,假设每次利用 CI/CD 时应用程序都会自动部署,我可以在管道中设置AS.NETCORE_ENVIRONMENT
的值吗?
As for, (2)
, I tried removing hardcoded secrets from appsettings.{environment}.json files and configured key vault like below but it doesn't work:至于, (2)
,我尝试从 appsettings.{environment}.json 文件中删除硬编码的秘密并配置密钥库,如下所示,但它不起作用:
builder.Services.Configure<KeyVaultConfig>(options => builder.Configuration.GetSection("KeyVaultConfig").Bind(options));
if (!string.IsNullOrEmpty(builder.Configuration["KeyVaultConfig:KeyVaultUrl"]))
{
var credentials = new ClientSecretCredential(builtConfig["KeyVaultConfig:TenantID"], builtConfig["KeyVaultConfig:ClientID"], builtConfig["KeyVaultConfig:ClientKey"]);
var secretClient = new SecretClient(new Uri(builtConfig["KeyVaultConfig:KeyVaultUrl"]), credentials);
//builder.Configuration.AddAzureKeyVault...
}
If in the end you are going to use CI/CD you can only need 2 files (appsettings.json and appsettings.Development.json) and the default C# handling of these.如果最后你打算使用 CI/CD,你只需要 2 个文件(appsettings.json 和 appsettings.Development.json)和默认的 C# 处理这些文件。
In the development one you hardcode all the configuration you need for dev.在开发中,您对开发所需的所有配置进行硬编码。 In the standard one, you put keys that would be replaced during the CI/CD pipelines.在标准模式中,您放置将在 CI/CD 管道期间替换的密钥。
for example例如
in appsettings.development.json you have在 appsettings.development.json 你有
"ConnectionStrings": {
"myDb": "Server=myDbServer;Database=myDb;User Id=userDev;Password=myPassword;"
}
in appsettings.json you have在 appsettings.json 你有
"ConnectionStrings": {
"myDb": "#{connectionString}#"
}
And then replace "#{connectionString}#" with the environment value in the CI/CD pipelines configuration然后将“#{connectionString}#”替换为 CI/CD 管道配置中的环境值
If you really want to have only one file, you could try Json variable substitution like documented here documented here during the CI/CD pipelines, but the first approach allows you to handle secrets with vault more easily如果您真的只想拥有一个文件,您可以尝试使用 Json 变量替换,就像在 CI/CD 管道中记录的此处记录的那样,但第一种方法允许您更轻松地处理保管库的秘密
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.