API - Feature Flags - App Configuration Service
API - Feature Flags - App Configuration Service

App Configuration – Using the feature flags from ASP .NET Core API

In last article, we have seen how to add a feature flag in the in Azure App Configuration Service. In this article, we are going to use the feature flag in ASP .NET Core API project.

Prerequisite

For following all steps in this article, we will need Azure Subscription. If you don’t have an Azure subscription, create a free account before you begin.

Create Azure App Configuration Service

We will need an instance of Azure App Configuration Service. You can refer this article to create one using Azure Portal. Alternatively, you can run the bicep script from this article to create one for you. Refer my GitHub repository to create the resource if you want to use the Bicep files.

Add Feature Flag

You can refer previous article to view how to add a new feature flag. We are going to need feature flag with name “StorageFeature“. The previous article adds the flag with same identifier.

We need to enable the flag and enter the text “first” in the label field.

Azure Portal – Add a feature flag with label

Create API Project

Now, let’s create an ASP .NET Core Web API Project, namely FeatureFlagApi, targeting to .NET 7 using Visual Studio.

Create ASP .NET Core Web API using Visual Studio

Manage NuGet Packages

Now, in order to access the feature flags stored in Azure App Configuration service from API, we will need to add a reference to below mentioned two NuGet packages

Setup Local Secret Manager

The commands given below shows how local secret manager can be setup. This will avoid accidentally pushing the secrets in GIT repository. We can also choose to use environment variables if that is preferred by your design.

Modify Startup

Now, let’s modify the startup configuration of the API. Open Program.cs file and add code so that the API process can connect with Azure App Configuration, by adding AddAzureAppConfiguration call in the startup. This time, we are passing a parameter in this call, of type Action<AzureAppConfigurationOptions>. In the passed parameter we are calling two methods

  • Connect to connect to pass the connection string of App Configuration Service
  • UseFeatureFlags, this method to load feature flags from App Configuration Service.

When no parameter is passed to the UseFeatureFlags method, it loads all feature flags with no label in your App Configuration store.   If we do not pass parameter to this method, then it will not load the StorageFeature flag. This is because, we have provided the text “first” in the label field while creating the feature flag. That’s why the code given below sets the label property of the feature flag options to the same label value.

The default refresh expiration of feature flags is 30 seconds. You can customize this behavior via the FeatureFlagOptions parameter.  In the code given below, we have set the expiration of cached flag values to be 45 seconds.

Then we also have registered ASP .NET Core’s feature management by calling AddFeatureManagement. For the ASP.NET Core feature management API reference documentation, see Microsoft.FeatureManagement Namespace.

Using Feature Flag

Let’s add a new controller, let’s name FeatureFlagController. In this controller, let’s inject IFeatureManager in the constructor as shown in the code snippet given below. On the IFeatureManager instance, the method IsEnabledAsync can be called. The method takes a string as parameter. The string should be basically the identifier from the feature flag configuration. Let’s pass the string "StorageFeature" as parameter. This method returns the Boolean value, indicating if the flag is enabled.

We are going to write a simple demo action, which is supposed to get data from the new feature if the flag is enabled. Just for the demonstration, I am returning HTTP 200 response and the response body would contain the string indicating the value of feature flag.

Run and Verify

Now, it is the time to verify the code that we have written. Let’s run the project and it should show the swagger UI. From there, let’s invoke the GET endpoint from FeatureFlagController. It should return the response based on the value of the feature flag.

Run and Verify – the feature flags from Azure App Configuration Service

Wrapping Up

You can find the complete source code on my GitHub repository. I hope you find this information helpful. Let me know your thoughts.

Leave a ReplyCancel reply