Feature flag in ASP.NET Core using Azure App Configuration

Posted on January 8, 2023
.NET Coreazuredotnetfeatureflagappconfiguration

What is a feature flag?

Feature flag can be used to enable disable specific functionalities of our application by turning a feature toggle on or off, without deploying code. 

What is Microsoft App Configuration?

Azure App Configuration provides a service to centrally manage application settings and feature flags. Modern programs, especially programs running in a cloud, generally have many components that are distributed in nature. Spreading configuration settings across these components can lead to hard-to-troubleshoot errors during an application deployment 

There are many benefits when using feature flags. Below is a couple of them:

  • Quickly hiding a badly developed functionality or a failure 
  • Delivering a feature to a specific user group 
  • Assist developers when doing trunk-based development 
  • Quicker release cycles 
  • Testing a new feature in a production environment

 

Please find the full code in github repository

How to setup App Configuration in Azure

STEP 01: Create a Free Trail account in the Azure portal Start Free 

STEP 02: Navigate to App Configuration -> Create App Configuration 

Create App Configuration

STEP 03: Fill in the resource information and click “Review & Create”, make sure to select the Pricing tier “Free” 

STEP 04: Add feature flag through feature manager -> Create 

Add feature flag through feature manager

Feature Manager

STEP 05: Find the access keys to make a connection through the .net core application Access keys

Create Console Application

_Now let’s create a Console application where we will read this newly created feature flag and enable or disable a part of the code based on its value. _

Install the following NuGet Packages that our application will use:

Microsoft.Extensions.DependencyInjection
Microsoft.Azure.AppConfiguration.AspNetCore
Microsoft.FeatureManagement
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration;
using Microsoft.FeatureManagement;


Console.WriteLine("Azure App Configuration Article Demo\n");


//Retrieve the Connection String from Azure App Configuration Resource
const string connectionString = "{Azure_App_ConnectionString}";


var configuration = new ConfigurationBuilder()
            .AddAzureAppConfiguration(options =>
            {
                options.Connect(connectionString).UseFeatureFlags();
            }).Build();


var services = new ServiceCollection();


services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement();


using (var serviceProvider = services.BuildServiceProvider())
{
    var featureManager = serviceProvider.GetRequiredService<IFeatureManager>();


    //read great day feature
    if (await featureManager.IsEnabledAsync("TogglePreviewFeature"))
    {
        Console.WriteLine("Preview Feature Enabled!!!");
    }
    else
    {
        Console.WriteLine("Preview Feature Disabled!");
    }
};

The Last thing is to replace the {your_app_configuration_connection_string} with the connection string from the Azure App Configuration, which can be found under Settings -> Access keys. 

Please find the full code in github repository

Output

www.arunyadav.in Output

Thanks for reading!


Posted on January 8, 2023

Anonymous User

January 9, 2023

Great Information with clean walkthrough. Thanks Arun

Profile Picture

Arun Yadav

Software Architect | Full Stack Web Developer | Cloud/Containers

Subscribe
to our Newsletter

Signup for our weekly newsletter to get the latest news, articles and update in your inbox.

More Related Articles