This is part five in a series of articles.
EDIT: my Feature Management Pluralsight training course is now available.
So far in this series the feature flags in the application have been managed via the appsettings.json file, for example:
This means that if you want to manually enable a feature you need to update the config file manually in production or release a new version to production with the changed config file. If you want to manage feature flags without needing to modify the running production app/config one thing you could do is to create a custom feature filter to read from a database or API that’s external to your app.
As an alternative you could use Azure App Configuration to manage your features. This enables you to turn features on or off whenever you want.
Creating an Azure App Configuration Store
The first thing to do is sign in to Azure or create a free account.
Once in the Azure Portal, click the Create a resource button, and search for App Configuration:
Click the Create button.
Next, configure your App Configuration store, for example:
Notice in the preceding screenshot that I’ve selected the Pricing tier as Free which comes with a number of limitations such as no SLA, etc. – there is also the Standard tier which comes with 99.9% availability SLA, more requests, etc.
Once you’ve configured the new store, click Create. This will submit the deployments, you may have to wait a short while and then if you head to All Resources in the portal you should see your new store listed - click on it an then choose Feature manager:
It’s here you can add the names of your feature flags.
For example let’s add a feature flag to control printing in the app. Click the Add button, choose Off or On, give the feature a name (key) of “Printing”, and a description of “Print preview features”:
You now have a Printing feature flag, the next thing is to use it in an app.
Using Azure App Configuration Feature Flags in ASP.NET Core
In an ASP.NET Core app, add the following NuGet packages:
Next add a new connection string in the appsettings.json file called “AppConfig”. The connection can be found in the Azure portal by clicking on Access Keys and then clicking the copy to clipboard button next to the connection string:
Paste this connection string into the appsettings.json, for example:
Note: you should be careful when adding secrets to config files that they are not accidentally checked in to source control or otherwise compromised. You should not have production passwords, secrets, etc checked into source control. One way to manage secrets in development to use the Secret Manager.
The next step is to modify the ASP.NET Core app to look for feature flags in Azure App Configuration.
First, modify the program.cs CreateHostBuilder method to add Azure App Configuration support:
public static IHostBuilder CreateHostBuilder(string args) =>
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
var settings = config.Build();
Notice in the preceding code that we are calling AddAzureAppConfiguration and then making sure that .UseFeatureFlags() is called.
Next in the Startup.Configuration method, add feature management:
public void ConfigureServices(IServiceCollection services)
Turning Feature Flags Off and On with Azure App Configuration
Now in the app we can reference the Printing feature. In part one of this series we saw the feature tag helper, for example to hide a menu item based on a Printing feature flag:
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Print">Print Preview</a>
Note: You should not rely solely on UI elements to “hide” features, you should also control them at the controller/action level with the [FeatureGate] attribute otherwise the action/URL will still be accessible even if the UI is not showing the menu item for example..
If you run the app now the printing menu item will not be shown because the Printing feature is currently off in Azure App Configuration:
If you click the On toggle, the feature will now be set to enabled. If you refresh the browser window you will notice however that the printing menu item is still not shown. However if you stop the web app and restart it the printing menu will now be shown.
To enable “live” updating of feature flags in ASP.NET Core you need to add an extra piece of middleware, to do this modify the Startup.Configure method and add app.UseAzureAppConfiguration();
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
If you run the web app now and turn the feature off and on, if you refresh the browser a few times you should see the print menu item appear/disappear without needing to stop and start the web app. You may need to refresh several times before the feature flag setting is updated, this means that if you need to turn a feature off in an emergency situation it may not take effect immediately and you may still get users using the feature for a short while after making the change.
Be sure to check out my Microsoft Feature Management Pluralsight course get started watching with a free trial.