Skip to content

Latest commit

 

History

History
186 lines (125 loc) · 7.87 KB

quickstart-feature-flag-dotnet.md

File metadata and controls

186 lines (125 loc) · 7.87 KB
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

Quickstart: Add feature flags to a .NET/.NET Framework console app

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.

Prerequisites

Add a feature flag

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.

[!div class="mx-imgBorder"] Enable feature flag named Beta

Create a console app

You can use Visual Studio to create a new console app project.

  1. Start Visual Studio, and select File > New > Project.

  2. 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.

  3. 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.

Use the feature flag

  1. 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.

  2. Open Program.cs and add the following statements.

    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
    using Microsoft.FeatureManagement;
  3. Connect to App Configuration, specifying the UseFeatureFlags option so that feature flags are retrieved. Create a ConfigurationFeatureDefinitionProvider to provide feature flag definition from the configuration and a FeatureManager to evaluate feature flags' state. Then display a message if the Beta 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!");
    }

Build and run the app locally

  1. 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"
    

  2. Restart Visual Studio to allow the change to take effect.

  3. Press Ctrl + F5 to build and run the application.

  4. You should see the following outputs in the console.

    App with feature flag disabled

  5. Sign in to the Azure portal. Select All resources, and select the App Configuration store that you created previously.

  6. Select Feature manager and locate the Beta feature flag. Enable the flag by selecting the checkbox under Enabled.

  7. Run the application again. You should see the Beta message in the console.

    App with feature flag enabled

Clean up resources

[!INCLUDE azure-app-configuration-cleanup]

Next steps

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