title | titleSuffix | description | services | author | ms.service | ms.devlang | ms.custom | ms.topic | ms.tgt_pltfrm | ms.date | ms.author |
---|---|---|---|---|---|---|---|---|---|---|---|
Quickstart for adding feature flags to .NET/.NET Framework apps |
Azure App Configuration |
Learn to implement feature flags in your .NET application using feature management and Azure App Configuration. Dynamically manage feature rollouts, conduct A/B testing, and control feature visibility without redeploying the app. |
azure-app-configuration |
zhiyuanliang-ms |
azure-app-configuration |
csharp |
devx-track-csharp, mode-other, devx-track-dotnet |
quickstart |
.NET |
2/19/2024 |
zhiyuanliang |
In this quickstart, you incorporate Azure App Configuration into a .NET console app to create an end-to-end implementation of feature management. You can use App Configuration to centrally store all your feature flags and control their states.
The .NET Feature Management libraries extend the framework with feature flag support. These libraries are built on top of the .NET configuration system. They integrate with App Configuration through its .NET configuration provider.
- An Azure account with an active subscription. Create one for free.
- An App Configuration store. Create a store.
- Visual Studio
- .NET SDK 6.0 or later for .NET console app.
- .NET Framework 4.7.2 or later for .NET Framework console app.
Add a feature flag called Beta to the App Configuration store and leave Label and Description with their default values. For more information about how to add feature flags to a store using the Azure portal or the CLI, go to Create a feature flag.
You can use Visual Studio to create a new console app project.
-
Start Visual Studio, and select File > New > Project.
-
In Create a new project, filter on the Console project type and select Console App. If you want to create a .NET Framework app, please select Console App (.NET Framework) instead. Click Next.
-
In Configure your new project, enter a project name. If you are creating a .NET Framework app, please select .NET Framework 4.7.2 or higher under Framework. Click Create.
-
Right-click your project, and select Manage NuGet Packages. On the Browse tab, search and add the following NuGet packages to your project.
Microsoft.Extensions.Configuration.AzureAppConfiguration Microsoft.FeatureManagement
Make sure that the version of
Microsoft.FeatureManagement
is greater than 3.1.0. -
Open Program.cs and add the following statements.
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration.AzureAppConfiguration; using Microsoft.FeatureManagement;
-
Connect to App Configuration, specifying the
UseFeatureFlags
option so that feature flags are retrieved. Create aConfigurationFeatureDefinitionProvider
to provide feature flag definition from the configuration and aFeatureManager
to evaluate feature flags' state. Then display a message if theBeta
feature flag is enabled.IConfiguration configuration = new ConfigurationBuilder() .AddAzureAppConfiguration(options => { options.Connect(Environment.GetEnvironmentVariable("ConnectionString")) .UseFeatureFlags(); }).Build(); IFeatureDefinitionProvider featureDefinitionProvider = new ConfigurationFeatureDefinitionProvider(configuration); IVariantFeatureManager featureManager = new FeatureManager( featureDefinitionProvider, new FeatureManagementOptions()); if (await featureManager.IsEnabledAsync("Beta")) { Console.WriteLine("Welcome to the beta!"); } Console.WriteLine("Hello World!");
public static async Task Main(string[] args) { IConfiguration configuration = new ConfigurationBuilder() .AddAzureAppConfiguration(options => { options.Connect(Environment.GetEnvironmentVariable("ConnectionString")) .UseFeatureFlags(); }).Build(); IFeatureDefinitionProvider featureDefinitionProvider = new ConfigurationFeatureDefinitionProvider(configuration); IVariantFeatureManager featureManager = new FeatureManager( featureDefinitionProvider, new FeatureManagementOptions()); if (await featureManager.IsEnabledAsync("Beta")) { Console.WriteLine("Welcome to the beta!"); } Console.WriteLine("Hello World!"); }
-
Set an environment variable named ConnectionString to the connection string of your App Configuration store.
If you use the Windows command prompt, run the following command.
setx ConnectionString "connection-string-of-your-app-configuration-store"
Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it's set properly.
If you use Windows PowerShell, run the following command.
$Env:ConnectionString = "connection-string-of-your-app-configuration-store"
-
Restart Visual Studio to allow the change to take effect.
-
Press Ctrl + F5 to build and run the application.
-
You should see the following outputs in the console.
-
Sign in to the Azure portal. Select All resources, and select the App Configuration store that you created previously.
-
Select Feature manager and locate the Beta feature flag. Enable the flag by selecting the checkbox under Enabled.
-
Run the application again. You should see the Beta message in the console.
[!INCLUDE azure-app-configuration-cleanup]
In this quickstart, you created a feature flag in App Configuration and used it with a console app. To learn how to dynamically update feature flags and other configuration values without restarting the application, continue to the next tutorial.
[!div class="nextstepaction"] Enable dynamic configuration in a .NET app
[!div class="nextstepaction"] Enable dynamic configuration in a .NET Framework app
To enable feature management capability for other types of apps, continue to the following tutorials.
[!div class="nextstepaction"] Use feature flags in ASP.NET Core apps
[!div class="nextstepaction"] Use feature flags in .NET background services
For the full feature rundown of the .NET feature management library, continue to the following document.
[!div class="nextstepaction"] .NET Feature Management