App Config Store - Feature Flags - Isolated Function Apps
App Config Store - Feature Flags - Isolated Function Apps

App Configuration – Use Feature Flags in Isolated Azure Functions 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 in isolated Azure Functions app

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

Create Azure Functions Project

We can create an Azure Functions project using Visual Studio. Let’s say the name of the project is FeatureFlagIsolatedFunctions. The snapshot given below shows the settings that I have selected while creating the project. You can refer this article for detailed steps to create Azure Functions project using Visual Studio.

Note that functions worker is set to .NET 7 Isolated as shown in the snapshot below. Also, the authorization level is set to Anonymous for this demo. Also, the function is Http Triggered function.

Visual Studio – Creating new functions app project

Manage NuGet Packages

Now, let’s navigate to the Solution Explorer and right click on the project. Select Manage NuGet Packages option from the context menu. In the Browse tab, search for below mentioned packages and install them.

Modify Function Startup

Once the packages are installed, the ConfigureAppConfiguration should be added in the startup code in Program.cs. 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.

This time, 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 ConfigureServices, we also have opted for ASP .NET Core’s feature management by calling AddFeatureManagement. For the ASP.NET Core feature management API reference documentation, see Microsoft.FeatureManagement Namespace.

Modify the Function

Let’s move to the file which contains the function. There are three things that we need to modify:

  • The constructor should accept three dependencies – ILogger, IFeatureManagerSnapshot to read feature flags and IConfigurationRefresher to refresh the configuration.
  • The method should be modified. It should not need the parameter. The method should refresh the configuration and then check if feature flag is enabled. We are going to return the state of the feature flag in the response as a string.

The TryRefreshAsync method is called at the beginning of the Functions call to refresh feature flags. It will be a no-op if the cache expiration time window isn’t reached. Remove the await operator if you prefer the feature flags to be refreshed without blocking the current Functions call. In that case, later Functions calls will get updated value.

Set Environment Variable

In order to run the functions app project, we need to set the environment variable. Below are the commands which can be used for that purpose.

## Connection String Environment Variable via Command Prompt
setx ConnectionString "{app-config-store-connection-string}"

## Set Connection String Env. Variable via PowerShell
$Env:ConnectionString = "{app-config-store-connection-string}"

Run and Verify

After setting this variable, we can run the application and it should run successfully. Then we can call the endpoints via PowerShell script to ensure that the values from App Configurations are being read appropriately.

Windows PowerShell – Making Http GET Request to Azure Functions App

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

Leave a Reply Cancel reply