Skip to content

Files

Latest commit

e7854e1 · Feb 21, 2022

History

History
131 lines (93 loc) · 5.22 KB

quickstart-feature-flag-dotnet.md

File metadata and controls

131 lines (93 loc) · 5.22 KB
title description services documentationcenter author editor ms.assetid ms.service ms.devlang ms.custom ms.topic ms.tgt_pltfrm ms.workload ms.date ms.author
Quickstart for adding feature flags to .NET Framework apps | Microsoft Docs | Microsoft Docs
A quickstart for adding feature flags to .NET Framework apps and managing them in Azure App Configuration
azure-app-configuration
AlexandraKemperMS
azure-app-configuration
csharp
devx-track-csharp, mode-other
quickstart
.NET
tbd
10/19/2020
alkemper

Quickstart: Add feature flags to a .NET Framework app

In this quickstart, you incorporate Azure App Configuration into a .NET Framework app to create an end-to-end implementation of feature management. You can use the App Configuration service 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

Create an App Configuration store

[!INCLUDE azure-app-configuration-create]

  1. Select Feature Manager > +Add to add a feature flag called Beta.

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

    Leave label undefined for now.

Create a .NET console app

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

  2. In Create a new project, filter on the Console project type and click on Console App (.NET Framework). Click Next.

  3. In Configure your new project, enter a project name. Under Framework, select .NET Framework 4.8 or higher. Click Create.

Connect to an App Configuration store

  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.DependencyInjection
    Microsoft.Extensions.Configuration.AzureAppConfiguration
    Microsoft.FeatureManagement
    
  2. Open Program.cs and add the following statements:

    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Configuration.AzureAppConfiguration;
    using Microsoft.FeatureManagement;
    using System.Threading.Tasks;
  3. Update the Main method to connect to App Configuration, specifying the UseFeatureFlags option so that feature flags are retrieved. Then display a message if the Beta feature flag is enabled.

        public static async Task Main(string[] args)
        {         
            IConfigurationRoot configuration = new ConfigurationBuilder()
                .AddAzureAppConfiguration(options =>
                {
                    options.Connect(Environment.GetEnvironmentVariable("ConnectionString"))
                           .UseFeatureFlags();
                }).Build();
    
            IServiceCollection services = new ServiceCollection();
    
            services.AddSingleton<IConfiguration>(configuration).AddFeatureManagement();
    
            using (ServiceProvider serviceProvider = services.BuildServiceProvider())
            {
                IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
    
                if (await featureManager.IsEnabledAsync("Beta"))
                {
                    Console.WriteLine("Welcome to the beta!");
                }
            }
    
            Console.WriteLine("Hello World!");
            Console.WriteLine("Press any key to continue ...");
            Console.Read();
        }

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"

    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 console app.

    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 .NET Framework 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