Mastering Feature Flags in ASP.NET Core with IFeatureManager
Feature flags allow you to enable or disable features dynamically in ASP.NET Core. Understand their types, benefits, and how to implement them using IFeatureManager with minimal code.
Your company here — reach thousands of C# and ASP.NET professionals.Get in touch today!
Feature flags, also known as feature toggles, are a modern development technique that allows teams to control the availability of application features at runtime without deploying new code. ASP.NET Core supports feature flags via the IFeatureManager interface from the Microsoft.FeatureManagement package. This makes it possible to safely release new features, experiment with A/B testing, or temporarily disable functionality during failures or hotfixes.
The key principle behind feature flags is simple: instead of hardcoding behavior, you define flags in configuration, which your code can read at runtime to decide which logic path to follow. This approach separates feature deployment from code deployment, reducing risk and giving teams more flexibility.
Types of Feature Flags
Feature flags are not limited to simple on/off switches. ASP.NET Core supports several types of filters that determine how and when a feature is active:
- Boolean flag: The simplest type. A true/false value directly controls the feature.
- Time window filter: Enables a feature only during a specific time period, perfect for promotions or seasonal features.
- Percentage rollout: Gradually enables a feature for a percentage of users, supporting canary deployments and gradual rollouts.
- Targeting filters: Activate features for specific users, groups, or roles, useful for internal beta testing or selective experiments.
Custom filters can also be implemented for more complex rules, such as enabling features based on geographic location or subscription plans.
Minimal Example
A minimal way to check a feature flag in ASP.NET Core using IFeatureManager is:
using Microsoft.FeatureManagement;
public class DashboardService
{
private readonly IFeatureManager featureManager;
public DashboardService(IFeatureManager featureManager)
{
this.featureManager = featureManager;
}
public async Task<string> GetDashboardVersion()
{
return await featureManager.IsEnabledAsync("NewDashboard") ? "New" : "Legacy";
}
}
This snippet illustrates the core idea: a single call to IsEnabledAsync dynamically determines which feature path to execute.
Configuring Flags
Flags are typically configured in appsettings.json, allowing runtime control without touching code:
{
"FeatureManagement": {
"NewDashboard": true,
"HolidayPromo": {
"EnabledFor": [
{
"Name": "TimeWindow",
"Parameters": {
"Start": "2024-12-01T00:00:00Z",
"End": "2025-12-31T23:59:59Z"
}
}
]
}
}
}
This configuration enables a new dashboard globally, while the holiday promotion is active only within the defined time range.
Comparing Feature Flag Filters
Different filters serve different purposes. Here’s a quick comparison with example scenarios:
| Filter Type | Description | Use Case Example |
|---|---|---|
| Boolean | Simple true/false switch | Enable dark mode for all users |
| Time window | Active only between specific start and end dates | Holiday promotions or seasonal UI changes |
| Percentage rollout | Feature enabled for a percentage of users | Gradual rollout of a new recommendation engine |
| Targeting | Feature enabled for specific users or groups | Beta features for premium users only |
| Custom filters | Any custom logic, like geolocation or subscription tier | Regional content restrictions, enterprise-specific features |
This table helps teams choose the right strategy depending on risk, audience, and deployment scenario.
Best Practices for Large Projects
For projects with multiple teams and many features, feature flags can become complex. Here are recommendations for managing them effectively:
- Naming conventions: Use a consistent, descriptive pattern such as FeatureGroup_FeatureName to avoid collisions and clarify purpose.
- Lifecycle management: Track creation and retirement dates. Remove deprecated flags promptly to prevent technical debt.
- Environment differentiation: Flags may behave differently across dev, staging, and production. Maintain environment-specific configurations.
- Flag ownership: Assign clear ownership to features or teams responsible for enabling and monitoring them.
- Documentation: Maintain a central source that lists active flags, their purpose, filters, and related endpoints or modules.
- Limit active flags: Avoid excessive simultaneous flags, which complicate testing and increase cognitive load for developers.
By following these practices, teams can scale feature flag usage without introducing chaos, ensuring maintainability and clarity.
Benefits
Feature flags provide multiple advantages:
- Safe deployment: Release code with features disabled to reduce risk.
- Fast rollback: Disable problematic features without redeploying.
- Gradual rollout: Activate features for a subset of users before full release.
- Experimentation: Run A/B tests or trial new functionality safely.
- Operational control: Non-developers can toggle features through configuration.
Feature flags are a powerful tool that allows developers and product teams to control the rollout of features, reduce risk, and experiment safely, making them an essential part of modern ASP.NET Core applications.
Your company here — reach thousands of C# and ASP.NET professionals.Get in touch today!