Feature Flags - App Configuration - Console App
Feature Flags - App Configuration - Console App

App Configuration – Using feature flags with Console App

In one of the previous posts, 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 with console application, created using .NET 7.

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 – Adding a feature flag in App Configuration Service

Create Console App

Let’s create a new console app. Then we need to add three package references. So, right click on the project and select Manage NuGet Packages and select the Browse tab. Then search for the packages mentioned below:

Once these package references are added, we need to add the code given below.

The code first sets up dependency injection in the console application. Then it adds the dependencies required for connecting with Azure App Configuration service. The Action delegate here would read the connections string from environment variable and then it will use that connection string to setup connection with Azure App Configuration service instance.

We are passing a parameter in this call, of type Action<AzureAppConfigurationOptions>. In the passed parameter we are calling three methods

  • Connect, to connect to pass the connection string of App Configuration Service
  • Select(“_”), a dummy call to load a non-existing dummy key. This is to ensure that no other configuration apart from feature flags. If this method is not called, all the key-value pairs from the app configuration store will be loaded.
  • 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, under service collections, we also have opted for feature management by calling AddFeatureManagement. For the feature management API reference documentation, see Microsoft.FeatureManagement Namespace.

The code snippet given below shows the complete code from Program.cs. The code just prints the text indicating if the flag is enabled or not.

Run and Verify

Now, if we run the console app, we should be able to see the intended outcome.

Console Application – Showing that the feature flag is enabled

I hope you find this information helpful. Let me know your thoughts.

Leave a Reply Cancel reply

This Post Has 2 Comments

  1. Deepak K Upadhyay

    How do I get the updated flag value in real time? Say feature was enabled when the application started, but then I disabled the flag from azure portal but application still continues to get the flag as enabled even after well past the cache refresh time?

    1. Manoj Choudhari
      Manoj Choudhari

      As far as I know, it should work. When the feature flag status is changed and then the application is refreshed, the application should be able to get the latest changed value. I think probably you may want to check where the feature flags are being read. Or probably, your application may be caching the flags. These are just guesses, to help you troubleshoot this issue further. Hope this helps.