1

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

santosh kumar patro
  • 7,231
  • 22
  • 71
  • 143
  • This is an unusual request. Settings files are static while App Configuration settings can change after deployment. Adding a configuration source overrides the previous one, which is why adding App Configuration overrides `appsettings.json`. You can add *another* JSON file after adding App Configuration to override previous settings. There's nothing special to the name `appsettings.json`, that's just the default settings file name – Panagiotis Kanavos Jun 26 '23 at 11:26
  • Thanks @PanagiotisKanavos for your response. Here goes the code : var config = builder.Configuration; var appOptions = builder.Services.BindValidateReturn(config); // AzureAppConfiguration.Start builder.Configuration.AddAzureAppConfiguration(options => { options.Connect(config[key: AppKeys.AppConfigurationCnctnString]); }); // AzureAppConfiguration.End – santosh kumar patro Jun 26 '23 at 11:30

2 Answers2

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: enter image description here

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.

sa-es-ir
  • 3,722
  • 2
  • 13
  • 31
0

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.

Zhenlan Wang
  • 1,213
  • 8
  • 10