visualstuart/AspNetCore6WebApiWithFeatureFlags

★ 0Forks 0C#GitHub ↗Compare

README

Objectives

This sample adapts Quickstart: Add feature flags to an ASP.NET Core app | Microsoft Docs to an ASP.NET Core 6 Web API project, and adds some additional feature flag capabilities.

This sample demonstrates:

  • [FeatureGate] attribute to flag a controller action (can also be applied to a controller); see the WeatherForecastController.Get method.
  • Dependency injection of IFeatureManagerSnapshot to access feature flag values, and IConfigurationRefresherProvider used to reload feature flag values; see the WeatherForecastController constructor.
  • Using the injected dependencies to try refreshing (reloading) feature flag values, and using feature flag values; see the WeatherForecastController.Get method.

Additionally, the sample:

  • Ensures that only feature flags, and no other configuration values, are loaded; see the invocation of the AzureAppConfigurationOptions.Select method use in Program.cs using the "_" parameter.
  • Sets the feature flag cache expiration interval to an artificially low value (1 second) to facilitate local development.

Requirements

This sample requires:

  • An Azure App Configuration instance (see any of the Microsoft Quickstarts to set up an Azure App Configuration instance and clean it up when done)
    • With the connection string configured in local User Secrets (see steps used below)
    • and two feature flags named Beta and Gamma with no filtering.

The following steps were used to create project and set up the connection string to Azure App Config in local User Secrets.

dotnet new webapi
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
dotnet add package Microsoft.FeatureManagement.AspNetCore

dotnet user-secrets init
dotnet user-secrets set ConnectionStrings:AppConfig "your_connection_string"

See Program.cs and Controllers/WeatherForecastController.cs for configuring and using feature flags from Azure App Config.

References

Issues