Skip to content

Instantly share code, notes, and snippets.

@micklaw
Last active November 28, 2019 22:39
Show Gist options
  • Save micklaw/15455779cf898762f25ffe8399b976fb to your computer and use it in GitHub Desktop.
Save micklaw/15455779cf898762f25ffe8399b976fb to your computer and use it in GitHub Desktop.
Azure App Config: Create Feature Flag using Azure.Data.AppConfiguration namespace
namespace FeatureFlags.Extensions
{
using System;
using Azure;
using Azure.Data.AppConfiguration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.AzureAppConfiguration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.FeatureManagement;
using Newtonsoft.Json;
public static class AzureAppConfigExtensions
{
public static async Task InitialiseFlags(string connectionString, string label, Feature[] featureFlags)
{
var client = new ConfigurationClient(connectionString);
/* Loop the flags and create as disabled the first time */
foreach (var flag in featureFlags)
{
var featureModel = new CreateFeatureModel()
{
Enabled = false,
Id = flag.Id,
Description = flag.Description
};
var config = JsonConvert.SerializeObject(featureModel);
try
{
/* A feature flag needs this exact name as a prefix followed by your key */
/* It also needs the exact content type too or it will add as a config entry */
await client.AddConfigurationSettingAsync(new ConfigurationSetting(".appconfig.featureflag/" + flag.Id, config, label)
{
ContentType = "application/vnd.microsoft.appconfig.ff+json;charset=utf-8"
});
}
catch (RequestFailedException exception) when (exception.Status == 412)
{
/* If the exception is a 412, it more than likely already in the instance, so ignore it */
if (!exception.Message.ToLower().Contains("setting was already present"))
{
throw;
}
}
}
}
}
}
@micklaw
Copy link
Author

micklaw commented Nov 28, 2019

^^ Not sure if the async methods actually exists or not, sure would be nice oif they did, though this runs on app start anyway, so reckon you'd be ok with it being sync anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment