Azure App Configuration is a services that provides a central place to manage application settings, and it provides cool features like feature flags and auto refresh of settings when they are changed.
To access Azure App Configuration from your .NET Core application, you include the preview NuGet Package:
Microsoft.Extensions.Configuration.AzureAppConfiguration
PLEASE NOTE: As per 2020-02-14, Azure App Configuration is still in beta. You must check the “include prerelease” button in Visual Studio to see the NuGet package.
To connect to the Azure App Configuration you need a connection string.
Most of the guides and help pages expects you get the connection string from an environment variable, or that you create a web host application and can get the connection string from a web config file.
But if you would like to use Azure App Configuration together with a local appsettings.json file, there is no help available.
The challenge is that the connectionstring to the Azure App Configuration is not available before you read and build a IConfiguration reading from the local appsettings.json. So you need some way to split the calls to ConfigurationBuilder and then merge the 2 together.
THE SCENARIO:
Let’s assume that you have a local appsettings.json file and that file includes the connectionstring (and maybe even some more settings that are not to be tampered with by those with access to the Azure App Configurations):
{ "Logging": { "FileLogPath": "e:\somewhere\log_.txt" }, "ConnectionStrings": { "AzureAppConfiguration": "Endpoint=https://xxxxxxxx.azconfig.io;Id=xxxxxxxx;Secret=xxxxxxxx", } }
THE SOLUTION:
You then need to merge these settings with the settings from Azure App Configuration.
var localConfig = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json", false, true) .Build(); var configuration = new ConfigurationBuilder() .AddConfiguration(localConfig) .AddAzureAppConfiguration( options => { options.Connect(configuration.GetConnectionString("AzureAppConfiguration")); } ) .Build();
The localConfig variable contains the settings from appsettings.json. This configuration is then merged with the Azure App Configuration into the configuration variable, which is then the final IConfiguration that will be used in the application.
MORE TO READ:
- Quickstart: Create an ASP.NET Core app with Azure App Configuration from Microsoft
- Microsoft.Extensions.Configuration.AzureAppConfiguration NuGet package repository
- Configuration in ASP.NET Core from Microsoft
- Tutorial: Use dynamic configuration in a .NET Core app from Microsoft
- Use Azure App Configuration with ASP.NET Core Application by Ashish Patel