如何在.NET 7中使用apsettings.json文件覆盖Azure App Configuration中的特定属性?

huangapple go评论103阅读模式
英文:

How to override Azure App Configuration with apsettings.json file for certain properties using .NET 7?

问题

I have 4 .NET 7 微服务部署在 Azure Kubernetes 服务中。对于本地开发环境,每个微服务都有 appsettings.json 和 appsettings.Development.json 文件。在这 4 个微服务中,appsettings.json 文件中有一些属性是通用的,还有一些是唯一的。为了集中管理所有应用程序相关的属性,我考虑利用 Azure 应用配置。它允许将应用程序配置集中到一个地方,并让不同的应用程序使用它。

我可以通过在 Azure 密钥保管库中管理的连接字符串连接到 Azure 应用配置。

例如,我有下面的设置,不同的微服务有不同的值:

{
"AppOptions": {
"Name": "Microservice1.API"
},
"SwaggerOptions": {
"Title": "Microservice1 Service",
"Description": "Microservice1 服务的开放 API 文档。",
"Name": "xxxxxxx",
"Email": ""
}
}

对于 Microservice 2,上述值是不同的,其他两个微服务也是如此。

有人可以建议我如何覆盖 Azure 应用配置属性并考虑 appsettings.json 文件中的值吗?

如果有更好的解决方法,请与我分享。

有人可以帮助我解决这个问题吗?

英文:

I have 4 .NET 7 microservices deployed in Azure Kubernetes Service. For local development environment, each microservice has appsettings.json and appsettings.Development.json file. Among these 4 microservices there are few properties in the appsettings.json file which are common and few are unique. In order to manage all the application related properties centrally I thought of leveraging Azure App Configuration It allows to centralize application configuration to a single place and have different applications use it.

I am able to connect to the Azure App Configuration via the connectionstring which is managed in Azure Key Vault.

For example, I have the below setting which has different value for different microservice

{
 
  "AppOptions": {
    "Name": "Microservice1.API"
  },
  "SwaggerOptions": {
    "Title": "Microservice1 Service",
    "Description": "Open API Documentation of Microservice1 Service API.",
    "Name": "xxxxxxx",
    "Email": ""
  }
}

The above values are different in case of Microservice 2 and the same applies for other two microservices.

Can anyone suggest me what is the way to override the Azure App Configuration properties and consider the values in the appsettings.json file?

Please share with me in case there is any other better way to deal with this issue

Can anyone please help me to fix this issue

答案1

得分: 1

I think the best practice is using labeling for app configuration. so that you can define a label per microservices.

For example in app configuration:

如何在.NET 7中使用apsettings.json文件覆盖Azure App Configuration中的特定属性?

And in the code you can retrieve them like this:

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options. Connect(connectionString)
           .Select(KeyFilter.Any, "Microservice1");

});

The above code is for "Microservice1" and should be the same for other microservices with their label names.

英文:

I think the best practice is using labeling for app configuration. so that you can define a label per microservices.

For example in app configuration:
如何在.NET 7中使用apsettings.json文件覆盖Azure App Configuration中的特定属性?

And in the code you can retrieve them like this:

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options. Connect(connectionString)
           .Select(KeyFilter.Any, "Microservice1");

});

The above code is for Microservice1 and should be the same for other microservices with their label names.

答案2

得分: 0

在应用配置中,我建议你在键名前加上微服务的名称。例如,

键名
Microservice1:AppOptions:Name Microservice1.API
Microservice2:AppOptions:Name Microservice2.API

然后在每个微服务中,你加载以该微服务开头的键,并修剪掉该前缀。这样,你的应用代码就不必处理前缀。例如,

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options.Connect(connectionString)
           .Select("Microservice1:*", LabelFilter.Null)
           .TrimKeyPrefix("Microservice1:");

});

对于本地开发,你可以在调用 AddAzureAppConfiguration(...) 之后 调用 AddJsonFile("appsettings.json")。然后,appsettings.json 中的内容将覆盖从应用配置加载的内容。或者,如果你检测到处于开发环境,例如基于一个环境变量,你可以完全跳过调用 AddAzureAppConfiguration(...)

英文:

In App Configuration, I recommend you prefix the key name with the microservice name. For example,

Key Value
Microservice1:AppOptions:Name Microservice1.API
Microservice2:AppOptions:Name Microservice2.API

Then in each of your microservice, you load keys that start with that microservice and trim that prefix. This way, you app code doesn't have to deal with the prefix. For example,

builder.Configuration.AddAzureAppConfiguration(options =>
{
    options.Connect(connectionString)
           .Select("Microservice1:*", LabelFilter.Null)
           .TrimKeyPrefix("Microservice1:");

});

For local development, you can call AddJsonFile("appsettings.json") after calling the AddAzureAppConfiguration(...). Then what's in the appsettings.json will override what's loaded from App Configuration. Alternatively, you can skip calling AddAzureAppConfiguration(...) altogether if you detect you are in the dev environment, for example, based on an environment variable.

huangapple
  • 本文由 发表于 2023年6月26日 19:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定