Feature flags (also known as feature toggles) are a powerful technique that allows developers to modify system behavior without changing code. ASP.NET Core provides built-in support for feature management through Microsoft.FeatureManagement packages make implementing this pattern in your applications easier than ever.
Why Use Feature Flags?
Feature Management allows you to dynamically control the visibility and functionality of application features.
The benefits you can gain with feature management:
- Continuous Delivery: Deploy code to production while keeping features hidden until they’re ready
- A/B Testing: Roll out features to specific user segments to gather feedback
- Controlled Rollouts: Gradually enable features for different user groups
- Emergency Killswitches: Quickly disable problematic features without deploying new code
Getting Started with Feature Management
.NET provides a library for feature management for .NET and ASP.NET Core applications.
To start with Feature Management, add the NuGet package Microsoft.FeatureManagement.AspNetCore.
dotnet add package Microsoft.FeatureManagement.AspNetCore
Then, configure feature management in your Program.cs or Startup.cs.
builder.Services.AddFeatureManagement();
Feature flags can be configured in your appsettings.json:
{
"FeatureManagement": {
"DarkModeEnabled": true,
"SomeFeatureEnabled": false
}
}
You can check feature flags by injecting IFeatureManager:
app.MapGet("/get_view_settings", async (IFeatureManager featureManager) =>
{
bool darkModeEnabled = await featureManager.IsEnabledAsync("DarkModeEnabled");
return new ViewSettings(DarkModeEnabled: darkModeEnabled);
});
It’s a good practice to define feature flags as string variables in order to reference them from code:
public static class FeatureFlags
{
public static string DarkModeEnabled = "DarkModeEnabled";
public static string SomeFeatureEnabled = "SomeFeatureEnabled";
}
You can also check feature flags with the FeatureGate attribute to control whether a whole controller class or a specific action is enabled in MVC.
In the MVC view, you can use a <feature> tag to render content based on whether a feature flag is enabled.
Azure App Configuration
It’s a good practice to store the feature flags outside the application. A good choice is Azure App Configuration.
With Azure App Configuration, you’ll have all your feature flags in a centralized place. You’ll be able to disable/enable feature flags dynamically without redeployments. The service supports labels to have feature flags for each environment.
To configure the connection with Azure App Configuration, install the NuGet Microsoft.Azure.AppConfiguration.AspNetCore.
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
Then, configure feature management:
builder.Configuration.AddAzureAppConfiguration(options =>
options.Connect(
builder.Configuration["ConnectionStrings:AppConfig"])
.UseFeatureFlags());
builder.Services.AddAzureAppConfiguration();
And use it in the application:
app.UseAzureAppConfiguration();
Best Practices
- Keep Feature Flags Temporary
- Remove feature flags once features are fully rolled out
- Regularly audit your feature flags to prevent technical debt
- Use Meaningful Names
- Choose descriptive names that clearly indicate the feature’s purpose
- Document Your Features
- Maintain documentation about active feature flags
- Include expiration dates for temporary flags
- Handle Default Cases
- Always provide fallback behavior when features are disabled
- Consider using feature flags with default values
Conclusion
Feature management in ASP.NET Core provides a robust framework for implementing feature flags in your applications. By following these patterns and best practices, you can safely deploy new features, conduct experiments, and maintain better control over your application’s behavior in production.