Azure App Configuration
What is Azure App Configuration?
- Azure App Configuration provides a service to centrally manage application settings and feature flags.
Prerequisite
- Create an Azure Account
- Setup Azure CLI
- Log in with Azure CLI
- Create a resource group.
- To learn these steps, please check Azure CLI content.
Create Azure App Configuration
- Example code to create free Azure App Configuration:$ az appconfig create \--resource-group codesanook-example-resource-group \--location southeastasia \--name codesanook-example-app-config \--sku Free
List connection string
$ az appconfig credential list \--resource-group codesanook-example-resource-group \--name codesanook-example-app-config
Store JSON value in App Configuration
$ az appconfig kv set \--connection-string 'Endpoint=https://codesanook-example-app-config.azconfig.io;Id=xxx;Secret=xxx' \--key TestApp:Settings:Colors \--value [\"red\", \"green\", \"blue\"] \--content-type application/json
Consume configuration in .NET Generic host
Setup a new .NET project and add required Nuget package
- Create a .NET Console app project with the following command:$ dotnet new console --name AzureAppConfigurationExample
- CD to
AzureAppConfigurationExample
folder and add required Nuget package with the following command:$ dotnet add package Microsoft.Extensions.Hosting$ dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
Update code to consume Azure App Configuration
Open the project with VS Code.
Open
Program.cs
file and update the code as following:using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Hosting;using Microsoft.Extensions.Options;// Create generic host buildervar builder = Host.CreateDefaultBuilder(args);// Configure configuration sourcebuilder.ConfigureAppConfiguration(configurationBuilder =>{configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());configurationBuilder.AddJsonFile("appsettings.json", optional: true);configurationBuilder.AddEnvironmentVariables();configurationBuilder.AddUserSecrets<Program>();// Configure Azure App Configuration. We need to build configuration to a get connection string from a user secret.var configuration = configurationBuilder.Build();configurationBuilder.AddAzureAppConfiguration(configuration.GetConnectionString("AppConfig"));});builder.ConfigureServices((hostContext, services) =>// Bind TestApp:Settings section to TestSettings class.services.Configure<TestSettings>(hostContext.Configuration.GetSection("TestApp:Settings")));// Build a generic host.using var host = builder.Build();// Consume the configuration by creating a new solve and resolve dependency.// IOptions can be used as constructor injectionusing var scope = host.Services.CreateScope();var settings = scope.ServiceProvider.GetRequiredService<IOptions<TestSettings>>();Console.WriteLine($"{string.Join("\n", settings.Value.Colors)}");// Run a generic host.await host.RunAsync();// Settings class to hold setting valuesclass TestSettings{public string[] Colors { get; set; }}
Set user secret to the project
- Get connection string value from
az appconfig credential list
command.$ dotnet user-secrets init$ dotnet user-secrets set ConnectionStrings:AppConfig 'Endpoint=https://codesanook-example-app-config.azconfig.io;Id=xxx;Secret=xxx'
Run .NET project
$ dotnet run
- You should see all values of colors setting in a terminal.
Delete Azure App Configuration key
$ az appconfig kv delete \--connection-string 'Endpoint=https://codesanook-example-app-config.azconfig.io;Id=xxx;Secret=xxx' \--key TestApp:Settings:Colors
Delete Azure App Configuration
- Example code to delete Azure App Configuration:
$ az appconfig delete \--resource-group codesanook-example-resource-group \--name codesanook-example-app-config
Useful resources
- Quick start: Create an ASP.NET Core app with Azure App Configuration
- Azure App Configuration CLI
- Using .NET Secret Manager with console applications
- Avoiding Startup service injection in ASP.NET Core 3
- Working with options and settings
- Comparing WebApplicationBuilder to the Generic Host
- Configuring named options using IConfigureNamedOptions and ConfigureAll
Loading comments...