简体   繁体   English

我们可以使用单个 appsettings.json 文件,而不是为不同的环境维护多个版本的 appsettings.{environmentname}.json 吗?

[英]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,我想实现两件事:

  1. Using a single appsettings.json file instead of maintaining multiple versions of appsettings.{environmentname}.json for different environments使用单个appsettings.json文件而不是为不同的环境维护多个版本的appsettings.{environmentname}.json
  2. Remove hard-coding from the 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.

相关问题 如何生成应用程序设置。<environmentname> .json 文件?</environmentname> - How to generate the appsettings.<EnvironmentName>.json file? 如何覆盖 appsettings.json 或 appsettings。<envrionment> .json 文件以编程方式使用 .net 核心 web api 和 C#?</envrionment> - How to overrite appsettings.json or appsettings.<envrionment>.json file(s) programtically using .net core web api with C#? appsettings.json 与 appsettings.{Environment}.json 在 .NET 核心应用 - appsettings.json vs appsettings.{Environment}.json in .NET Core apps Serilog 使用 appsettings.json 而不是 appsettings.Development.json - Serilog using appsettings.json instead of appsettings.Development.json 在数据库中保留实体,而不是appsettings.json - Persist Entitys in Database instead of appsettings.json 如何为不同的操作系统覆盖 appsettings.json? - How to override appsettings.json for different OS? 将appsettings.json发布到不同的环境 - Publishing appsettings.json to different environment 从不同的项目访问appsettings.json - Accessing appsettings.json from different projects 如何让我的 .NET Core 3 单文件应用程序找到 appsettings.json 文件? - How can I get my .NET Core 3 single file app to find the appsettings.json file? 通过 Windows 服务托管时加载 appsettings.&lt;&gt;.json 文件 - Loading appsettings.<>.json file when hosted via Windows Service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM