Manage Feature Flags using Azure App Configuration

Manage Feature Flags using Azure App Configuration

Why do we need to use Feature Flags?

Using Feature Flags, we can enable and disable specific functionalities of our application by turning their feature flag on or off, without deploying code.

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

  • Quickly hiding a bad 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 on a production environment

What is Azure App Configuration?

According to the official MS Docs (What is Azure App Configuration? | Microsoft Docs) Azure App Configuration provides a service to centrally manage application settings and feature flags. We can store all our feature flags in the Azure App Configuration and manage them in a single place. Additionally, we can access all the feature flags from any of our applications.

We can also use labels, to define different values for the same key.

Important: With Azure App Configuration we can control feature availability in real-time.

Now let’s take a look at how can we use all of the above mentioned in a simple example.

Project Structure?

No alt text provided for this image

Implementing simple feature flags functionality with Azure App Configuration

In order for us to define and use feature flags, first we need to create an Azure App Configuration resource.

For this, we need to go to the Azure Portal and search for App Configuration.

No alt text provided for this image

Now we can click on the ‘Create’ button to create a new App Configuration resource.?

No alt text provided for this image

Next, when creating the resource, fill out the information as below:

No alt text provided for this image

Here we choose the subscription we will use, and create a new Resource group where we will store our resource. We give our resource a name and choose a location. For the purpose of this demo, we will choose ‘Free’ for the pricing tier.

Click on ‘Review + create’ and then ‘Create’. Now we wait for our resource to be created.

No alt text provided for this image

After our resource creation succeeds, we can go and view it.

In order to use feature flags, we need to add some first. To add a new feature flag, we need to go to the Operations -> Feature Manager option shown on the image below:?

No alt text provided for this image

Opening it shows us the following page:

No alt text provided for this image

Clicking on the ’Add’ button brings us to a page where we can add our new feature flag. Let’s create the feature flag we will use in this demo application. First thing is to check the ’Enable feature flag’ option. Then, give it a name of ’greatdayinfo’, don’t specify a label, and add a description ’Have a great day!’. Click on the ’Apply’ button to create the feature flag.

No alt text provided for this image

After the feature flag is successfully created, we can see it in our feature flags list.

No alt text provided for this image

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.AspNet and Microsoft.FeatureManagement

Add the following code in the Program.cs file:

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 = "{your_app_configuration_connection_string}";


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("greatdayinfo"))
? ? {
? ? ? ? Console.WriteLine("Have a great day!!!");
? ? }
? ? else
? ? {
? ? ? ? Console.WriteLine("Please enable great day feature!");
? ? }
};        

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

No alt text provided for this image

Now let’s start our application with our feature flag enabled.

No alt text provided for this image

We can observe the following console output:

No alt text provided for this image

Now let’s disable the feature flag and run the application again.

No alt text provided for this image

Notice the output has changed accordingly.

No alt text provided for this image

Perfect. We have successfully managed to create a feature flag in Azure App Configuration and use it in our application to enable and disable a simple functionality.

Thanks for sticking to the end of another article from?"Iliev Talks Tech". #ilievtalkstech

The full, more detailed implementation of this example can be found on my GitHub repository on the following link:

DimitarIliev/Azure-App-Configuration: Simple application where we use Feature Flags from Azure App Configuration. (github.com)

This is going to be immensely powerful ??

This will come in very handy over the next few days ;)

要查看或添加评论,请登录

Dimitar Iliev ??的更多文章

社区洞察

其他会员也浏览了