Read and Update Azure App Configuration settings with C#

Azure App Configuration is a nifty way of storing configuration settings in the cloud, and with the live reload feature you can change the config settings and they will automatically update in your app without a need for redeploy.

But the configuration explorer is not the best tool in the world. It’s simple and does not give a very good overview of your settings. Especially if you use the labels feature.

This simple interface is not good. Especially with labels enabled.

But fear not. If you are an automation geek or just want to update the settings using code, this is – of course – possible.

STEP 1: THE NUGET PACKAGE

You will need this NuGet package:

STEP 2: CONNECT TO AZURE APP CONFIGURATION

// Connect to an App Configuration Client using a connectionstring
private static readonly string _connectionString = "Endpoint=https://mtaz-appconfig.azconfig.io...=";
private readonly ConfigurationClient _client = new ConfigurationClient(_connectionString);

STEP 3: READ ALL SETTINGS USING A FILTER

Filters use * notation to identity wildcards. In this example all my settings are named MySetting:[something], for example MySetting:FixCasing or MySetting:HideInSearch:

var settingsSelector = new SettingSelector() { KeyFilter = "MySetting:*" };
var settings = _client.GetConfigurationSettings(settingsSelector);

If you use labels, you can first sort by label, then get all settings:

public dynamic GetAllSettings()
{
  var settingsSelector = new SettingSelector() { KeyFilter = "MySetting:*" };
  var settings = _client.GetConfigurationSettings(settingsSelector);
  var labels = settings.GroupBy(s => s.Label);

  List<dynamic> list = new List<dynamic>();
  foreach (var label in labels)
  {
    dynamic s = new ExpandoObject();
    s.label = label.Key != null ? label.Key : string.Empty;
    var setingsByLabel = settings.Where(s => s.Label == label.Key);
    var values = new Dictionary<string, object>();
    foreach (var setting in setingsByLabel)
    {
      values.Add(setting.Key, setting.Value);
    }
    s.values = values;
    list.Add(s);
  }
  return list;
}

When serialized, the above returns the following output:

[
  {
    "label": "Development",
    "values": {
      "MySetting:ConvertCategoryToGroup": "false",
      "MySetting:FieldToGetCategoryFrom": "title",
      "MySetting:FixCasing": "false"
    }
  },
  {
    "label": "Production",
    "values": {
      "MySetting:ConvertCategoryToGroup": "false",
      "MySetting:FieldToGetCategoryFrom": "description",
      "MySetting:FixCasing": "false"
    }
  }
]

STEP 4: ADD, UPDATE AND DELETE SETTINGS

This is equally easy:

// Will add a setting it the setting is not already there
public void AddConfiguration(string key, string, value, string label)
{
  _client.AddConfigurationSetting(value.Key, value.Value, label);
}

// Will add a setting it the setting is not already there, 
// or update the setting if it already exist  
public void UpdateConfiguration(string key, string, value, string label)
{
  _client.SetConfigurationSetting(value.Key, value.Value, label);
}

// Deletes a single configuration setting
public void DeleteConfiguration(string key, string label)
{
  _client.DeleteConfigurationSetting(setting.Key, label);
}

// Deletes all settings matching my filter from a labeled section	
public void DeleteEntireConfigurationSection(string label)
{
  var settingsSelector = new SettingSelector() { KeyFilter = "MySetting:*", LabelFilter = label };
  var settings = _client.GetConfigurationSettings(settingsSelector);
  foreach (var setting in settings)
    _client.DeleteConfigurationSetting(setting.Key, label);
}

The DeleteEntireConfigurationSection is especially nifty, as you can wipe all settings from an entire labeled section.

That’s it. You are now an Azure App Configuration Expert. Happy coding.

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, .NET Core, c#, General .NET, 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 )

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.