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.Getmethod. - Dependency injection of
IFeatureManagerSnapshot
to access feature flag values, and
IConfigurationRefresherProvider
used to reload feature flag values; see the
WeatherForecastControllerconstructor. - Using the injected dependencies to try refreshing (reloading) feature flag values, and using feature flag values;
see the
WeatherForecastController.Getmethod.
Additionally, the sample:
- Ensures that only feature flags, and no other configuration values, are loaded;
see the invocation of the
AzureAppConfigurationOptions.Selectmethod 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.
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
BetaandGammawith 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.
- Quickstart: Create a .NET Core app with App Configuration | Microsoft Docs
- The main starting point for this sample.
This sample uses a
webapiproject instead of amvcapplication template, however that doesn't affect the use of feature flags in the example.
- The main starting point for this sample.
This sample uses a
- Quickstart: Add feature flags to an Azure Functions app | Microsoft Docs
- Illustrates suppressing loading other configuration and how to refresh (reload) feature flags.
- WebDemoNet6 in Azure / AppConfiguration / examples / DotNetCore on GitHub.
- What's new in ASP.NET Core 6.0 | Microsoft Docs, and particularly the "Web app template improvements" section.
- What's new in ASP.NET Core 5.0 | Microsoft Docs, and particularly the "Web API" section.