Azure App Configuration – Getting the connection string from appsettings.json

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.

Azure App Configuration

Azure App Configuration

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:

About briancaos

Developer at Pentia A/S since 2003. Have developed Web Applications using Sitecore Since Sitecore 4.1.
This entry was posted in .NET Core, c#, Microsoft Azure and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.