Feature flags (also known as feature toggles) have become a critical tool in modern software development, enabling developers to dynamically control the availability of application features. With the release of .NET 8, developers gain access to enhanced support for feature flags, seamlessly integrated with Azure’s powerful configuration and monitoring services. This article will guide you through implementing feature flags in .NET 8 with Azure, complete with coding examples and practical insights.
What Are Feature Flags?
Feature flags allow developers to enable or disable features at runtime without deploying new code. This capability provides numerous advantages, including:
- Continuous Deployment: Ship code to production without exposing incomplete or experimental features.
- A/B Testing: Test features with specific user groups before a full rollout.
- Quick Rollback: Disable problematic features instantly if issues arise.
- Granular Control: Enable features based on user roles, geographies, or other criteria.
In .NET 8, feature flags can be managed more effectively by leveraging integrations with Azure App Configuration and Feature Management libraries.
Setting Up Feature Flags in .NET 8
To demonstrate the implementation of feature flags, we’ll walk through an example of toggling a “BetaFeature” in a web application.
Prerequisites
- .NET 8 SDK installed.
- An Azure account.
- Visual Studio or your preferred code editor.
Step 1: Create a .NET 8 Web Application
# Create a new ASP.NET Core project
> dotnet new webapp -n FeatureFlagDemo
> cd FeatureFlagDemo
Step 2: Add Required NuGet Packages
Install the necessary packages for Azure App Configuration and feature management:
> dotnet add package Microsoft.FeatureManagement.AspNetCore
> dotnet add package Azure.Data.AppConfiguration
Step 3: Set Up Azure App Configuration
- Log in to the Azure portal and create an App Configuration resource.
- In the App Configuration resource, navigate to Feature Manager and add a new feature flag named
BetaFeature
. - Copy the connection string from the Access Keys section.
Step 4: Configure the Application
Update your appsettings.json
to include the Azure App Configuration connection string:
{
"AzureAppConfiguration": {
"ConnectionString": "<your-connection-string>"
}
}
Modify Program.cs
to integrate feature management:
using Azure.Identity;
using Microsoft.FeatureManagement;
var builder = WebApplication.CreateBuilder(args);
// Load Azure App Configuration
builder.Configuration.AddAzureAppConfiguration(options =>
{
options.Connect(builder.Configuration["AzureAppConfiguration:ConnectionString"])
.UseFeatureFlags();
});
// Add services
builder.Services.AddFeatureManagement();
var app = builder.Build();
// Use middleware
app.UseAzureAppConfiguration();
app.MapGet("/", async context =>
{
var featureManager = context.RequestServices.GetRequiredService<IFeatureManager>();
if (await featureManager.IsEnabledAsync("BetaFeature"))
{
await context.Response.WriteAsync("Beta feature is enabled!");
}
else
{
await context.Response.WriteAsync("Beta feature is disabled.");
}
});
app.Run();
Step 5: Test the Application
Run the application locally:
> dotnet run
Navigate to http://localhost:5000
and observe the behavior based on the state of the BetaFeature
flag in Azure.
Advanced Feature Management
Targeting Specific Users
Azure App Configuration supports targeting specific users or groups. You can define conditions in the Feature Manager by setting filters such as TargetingFilter
.
Example configuration in Azure:
{
"Audience": {
"Groups": [
{ "Name": "BetaTesters", "RolloutPercentage": 50 }
],
"Users": ["user1@example.com", "user2@example.com"]
}
}
Custom Feature Filters
You can create custom filters for advanced scenarios, such as enabling features based on time or complex business rules.
Custom Filter Example
using Microsoft.FeatureManagement;
public class TimeWindowFeatureFilter : IFeatureFilter
{
public Task<bool> EvaluateAsync(FeatureFilterEvaluationContext context)
{
var settings = context.Parameters.Get<TimeWindowSettings>();
var now = DateTime.UtcNow;
return Task.FromResult(now >= settings.Start && now <= settings.End);
}
}
public class TimeWindowSettings
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
}
Register the filter in Program.cs
:
builder.Services.AddFeatureManagement()
.AddFeatureFilter<TimeWindowFeatureFilter>();
Configure the filter in Azure App Configuration:
{
"Name": "TimeWindowFilter",
"Parameters": {
"Start": "2025-01-01T00:00:00Z",
"End": "2025-01-31T23:59:59Z"
}
}
Monitoring and Diagnostics
Azure provides tools to monitor and manage feature flags:
- Azure Monitor: Tracks changes to feature flags and their usage.
- Application Insights: Logs feature evaluations and their impact on application behavior.
- Rollback Support: Revert feature state changes instantly through the Azure portal or CLI.
Benefits of Using Feature Flags with .NET 8 and Azure
- Scalability: Manage feature flags across distributed applications with ease.
- Integration: Seamlessly integrate with Azure services for enhanced observability.
- Flexibility: Use custom filters and targeting for granular control.
- Developer Productivity: Simplify testing and deployment processes.
Conclusion
Feature flags in .NET 8, combined with Azure App Configuration, empower developers to build resilient and adaptable applications. By decoupling feature rollout from code deployment, teams can iterate faster, reduce risk, and deliver exceptional user experiences. Whether you’re managing a global-scale application or experimenting with new features, the combination of .NET 8 and Azure offers a robust, flexible, and efficient solution.