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 InProcess 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.
Create Azure Functions Project
We can create an Azure Functions project using Visual Studio. Let’s say the name of the project is FeatureFlagFunction. 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 authorization level is set to Anonymous for this demo. Also, the function is Http Triggered function. Ensure that you select appropriate option to enable Open API support.
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.
Microsoft.Extensions.Configuration.AzureAppConfiguration
version 4.1.0 or laterMicrosoft.FeatureManagement
version 2.2.0 or laterMicrosoft.Azure.Functions.Extensions
version 1.1.0 or later
Modify Function Startup
Now, let’s add a new file, Startup.cs, with the code given in the snapshot given below. It defines a class named Startup
that implements the FunctionsStartup
abstract class. An assembly attribute is used to specify the type name used during Azure Functions startup.
The ConfigureAppConfiguration
method is overridden and Azure App Configuration provider is added as an extra configuration source by calling AddAzureAppConfiguration()
. 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 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 andIConfigurationRefresher
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
Let’s run the functions app project. Then open the Swagger UI page. You can find the URL of Swagger UI on the Functions console. Now, let’s trigger the endpoint that we have, which reads the feature flag. It should return appropriate response suggesting that the feature flag is enabled.
I hope you find this information helpful. Let me know your thoughts.