Azure App Configuration With Dotnet(C#)


Azure app configuration in c# dotnet

  • Azure App Configuration is a fully managed service, which provides you a way to store the configurations in a centralized store. You can store configurations of multiple apps in a single place.
  • It is really helpful for cloud native application (eg. Microservices), which runs on multiple virtual machines and use multiple external services.
  • It allows us to store Key-value pairs, manage feature flags, and organize configurations using labels and namespaces.
  • The main benefit is, it supports dynamic refreshing. Which means, you can change config values without restarting the app.
  • You can also add reference of azure key vault secret here.

Let’s see these things in action.

Dontet core web api app

  • Create a new dotnet core web api application
  • In appsettings.js, add following lines:
"Color": "Color from appsettings",
"Person": {
  "Name": "Name from appsettings"
}
  • In program.cs create an endpoint:
app.MapGet("/", (IConfiguration config) =>
{
    string color = config.GetSection("Color").Value ?? "No color";
    string name = config.GetSection("Person:Name").Value ?? "No name";
    return Results.Ok(new
    {
        color,
        name,
        message = "Hello.."
    });
});
  • Run the application and in the browser or postman, visit the root endpoint eg. https://localhost:7040/ you will notice:
{
  "color": "Color from appsettings",
  "name": "Name from appsettings",
  "message": "Hello.."
}

Let’s try to load these settings from azure app configuration.

Create a resource azure app configuration

Search app configuration

create app configuration

Adding config values

There are two ways to create it

👉1. Create manually

manually create key value

👉2. By import/export. We are going to use this option.

In source file, click on browse and browse the appsettings.json, which is located in your project.

import/export

This step will import everything from appsettings.json. Delete the unnecessary configs.

import/export

Click on three dots ‘…’ of each key-value and edit its values.

import/export

Configure the app configuration in a dotnet app

Nuget Packages

Add the following nuget package.

Microsoft.Azure.AppConfiguration.AspNetCore

Connection string

"ConnectionStrings": {
  "AppConfig": "<your-connection-string>"
}

Copy this value from here.

import/export

Load cofiguration

Add these lines in Program.cs

string appConfigConnectionString = builder.Configuration.GetConnectionString("AppConfig")
    ?? throw new InvalidOperationException("The connection string 'AppConfig' was not found.");

builder.Configuration.AddAzureAppConfiguration(appConfigConnectionString);

Now, run the application and in the browser or postman, visit the root endpoint eg. https://localhost:7040/ and you will notice:

{
  "color": "Color from app config",
  "name": "Name from appconfig",
  "message": "Hello.."
}

These values are loaded from App Configuration.

Loading keys with prefixes

Since this can be a centralized config store, which may be shared with multiple apps. You want to name keys that make sense and you can easily identify them. Eg. DotnetSample:Person:Name and DotnetSample:Color says that these are the key of Dotnet Sample app.

import/export

To retrieve this you have to change your configuration in Program.cs.

builder.Configuration.AddAzureAppConfiguration(options =>
{
   options.Connect(appConfigConnectionString)
     .Select("DotnetSample:*");
});

In this ways you loads the configs that is only relevant to your application.

It will load the config keys that starts with DotnetSample:.

Endpoint:

app.MapGet("/", (IConfiguration config) =>
{
   string color = config.GetSection("DotnetSample:Color").Value ?? "No color";
   string name = config.GetSection("DotnetSample:Person:Name").Value ?? "No name";
   return Results.Ok(new
   {
       color,
       name,
       message = "Hello.."
   });
});

You can also modify appsettings.json, if you want to load different config in dev environment and different config in production environment.

appsettings:

 "DotnetSample": {
  "Color": "Color from appsettings",
  "Person": {
    "Name": "Name from appsettings"
  }
}

Offcource, you also need to call builder.Configuration.AddAzureAppConfiguration only in production.

if (builder.Environment.IsProduction())
{
    builder.Configuration.AddAzureAppConfiguration(options =>
    {
        options.Connect(appConfigConnectionString)
          .Select("DotnetSample:*");
    });
}

More resources