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.
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
We need to enable the flag and enter the text “first” in the label field.
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.
I hope you find this information helpful. Let me know your thoughts.