Feature flags are very useful when you develop an application and while you don’t want users to use the feature that isn’t really ready, you don’t want to stay on a side branch for too long as merging and rebasing from the main branch can become quite the chore. Enter feature flags.

So how do we do it?

There are tons of libraries we can use to do this but before we get into that, a word of caution : Get Rid Of Your Feature Flags As Soon As You Don’t Need Them Anymore. Creating feature flags is easy, managing them can become a headache, especially when you have a list of them in your configuration files and/or in your code. The feature flag must be removed as soon as it is not needed anymore whether the feature is ready to be used, or is never going to be used or whatever reason you may find that makes hiding the feature unnecessary anymore.

As I’m a big fan of Azure, we will leverage it to create, manage and remove our feature flags with very little hassle if at all. To continue this guide, you need an azure account. You can create it here, it is free and you have access to a lot of different functions and infrastructures. Once you’re done, you can come back and we’ll go on.

In Azure

Once you’re in azure, you need to create your app configuration. This is basically very similar to a simple appsettings.json file but it’s hosted in Azure instead of being deployed with your application. There are many advantages to do this but it is well beyond the scope of this article.

To create an app configuration, you need to click on the create resource button and then search for app configuration.

Once you click on it, and then on the create button, you’ll get to choose which subscription to use, which resource group you want it to be a part of, its name, location and the pricing tier (between free and standard, I choose free 😁)

You’ll be asked for a confirmation and then you’ll have to wait for a few seconds for the app configuration to be created and deployed in Azure.
Before you move on, copy and paste the connection string to the resource. You can find it under Settings > Access Keys. You’re going to need it afterwards.

Create your first Feature Flag

In the resource you just created, under operations you’ll find the Feature manager. To create one, all you have to do is to click on the add button on the top of the blade (a blade is a section screen in the Azure dashboard).
You have to enter the name of the feature and a description.
An important concept on this screen is the toggle On/Off. When selecting On, you mark the feature as active. While this is something that you might need, I advise to always create the feature as Off and activate it only when you are ready.
Once you’re happy with the data you just filled in, click on the apply button on the bottom of the screen et voilà! The feature is created we can now go to Visual Studio, open our application and use our newly created Feature Flag.

Configure your application

Before we dive into the code, you need to tell the application to use the app settings feature in Azure. Now, we could just copy and paste the connection string in the appsettings.json, but we’re going to store it in the local user-secret repo in your application folder. To do so, all you need to do is open a terminal, go to your application folder and type this command.

dotnet user-secrets set ConnectionStrings:AppConfig <your connection string>

if you have this error : “Could not find the global property ‘UserSecretsId’ in MSBuild project”, run the dotnet user-secrets init first and run the command again.
You may also need to add double quotes to the connection string you copied from azure like so Endpoint=”https://yourconfig.azconfig.io;Id=yourid;Secret=yoursecret” other wise you may get this error :
Could not execute because the specified command or file was not found.
Possible reasons for this include:
You misspelled a built-in dotnet command.
You intended to execute a .NET Core program, but dotnet-user-secret does not exist.
You intended to run a global tool, but a dotnet-prefixed executable with this name could not be found on the PATH.


or that one

The term ‘Id=<something>’ is not recognized as the name of a
cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify
that the path is correct and try again.

In order to tell your application that it needs to use the Feature Flags configured in Azure, you need to
1. Install the following nuget package : Microsoft.Azure.Appconfiguration.AspNetCore and Microsoft.FeatureManagement.AspNetCore
2. have the following code in your program.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((hostingContext, config) =>
            {
                var settings = config.Build();
                config.AddAzureAppConfiguration(options =>
                {
                    options.Connect(settings["ConnectionStrings:AppConfig"]).UseFeatureFlags();
                });
            })
            .ConfigureWebHostDefaults(webBuilder =>
                {                    
                    webBuilder.UseStartup<Startup>();
                });

What does this code do?
The ConfigureAppConfiguration method is used to tell your app that you will have a configuration file and the AddAzureAppConfiguration method just tells it where to go look for the configuration. Note that you don’t have to configure anything else in order to get the connection string from the user-secrets you created earlier.

Now, we need to go to the startup.cs file and add a couple more setups.
In the ConfigureServices method, we need to add services.AddFeatureManagement(); and in the Configure method, we add the middleware app.UseAzureAppConfiguration();

Use the Feature Flags

We use the built-in Dependency Injection to inject the IFeatureManagementSnapshot dependency in order to use it in our web page.

We can now hide a section of our code behind an plain old if statement.

There are other ways to use the Feature Flags in a web application, like by using annotations and even directly in the razor code, but that’s going to be for another post.

You can find the code used in this post here https://github.com/HerraHak/FeatureFlagDemo.

Happy coding !