IFeatureManager từ gói Microsoft.FeatureManagement quản lý feature flags trong .NET.
Nó cho phép bạn chọn nhánh logic phù hợp trong khi ứng dụng đang chạy.
Mục lục
Ứng dụng:
- Kích hoạt tạm thời hoặc tắt nhanh một tính năng — ví dụ, trong quá trình phát hành bản mới hoặc hotfix
- Tắt các chức năng gặp vấn đề trong khi xảy ra sự cố
- A/B testing
- Các tình huống tạm thời, như các chương trình khuyến mãi
Cách hoạt động:
Đăng ký Feature Management trong DI, thêm các bộ lọc nếu cần. Ví dụ, TimeWindow kích hoạt một flag chỉ trong khoảng thời gian cụ thể
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.FeatureFilters;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddFeatureManagement()
// Nếu bạn cần bộ lọc cửa sổ thời gian, hãy thêm dòng sau
.AddFeatureFilter<TimeWindowFilter>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new() { Title = "Feature Flags Demo API", Version = "v1" });
});
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapControllers();
app.Run();
Đặt giá trị trong appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"FeatureManagement": {
"NewDashboard": true,
"HolidayPromo": {
"EnabledFor": [
{
"Name": "TimeWindow",
"Parameters": {
"Start": "2024-12-01T00:00:00Z",
"End": "2025-12-31T23:59:59Z"
}
}
]
}
}
}
Kiểm tra trạng thái flag qua IFeatureManager
using Microsoft.AspNetCore.Mvc;
using Microsoft.FeatureManagement;
namespace FeatureFlagsDemo.Api.Controllers;
[ApiController]
[Route("api/[controller]")]
public class DashboardController(IFeatureManager featureManager) : ControllerBase
{
[HttpGet]
public async Task<IActionResult> GetDashboard()
{
var useNewDashboard = await featureManager.IsEnabledAsync("NewDashboard");
if (useNewDashboard)
{
return Ok(new
{
Version = "New",
Data = GetNewDashboardData(),
});
}
return Ok(new
{
Version = "Legacy",
Data = GetLegacyDashboardData(),
});
}
[HttpGet("promo")]
public async Task<IActionResult> GetHolidayPromo()
{
if (!await featureManager.IsEnabledAsync("HolidayPromo"))
{
return Ok(new { Message = "No active promotions" });
}
return Ok(new
{
Message = "Holiday Special Offer!",
Discount = "50% OFF",
ValidUntil = "December 31, 2025",
PromoCode = "HOLIDAY2025"
});
}
private static object GetNewDashboardData() => new
{
Charts = new[]
{
new { Name = "Revenue Chart", Type = "Line", Interactive = true },
new { Name = "User Activity", Type = "Heatmap", Interactive = true },
new { Name = "Performance Metrics", Type = "Gauge", Interactive = true }
},
Layout = "Modern Grid",
Theme = "Dark Mode Available",
Features = new[] { "Real-time Updates", "Custom Widgets", "Export Options" }
};
private static object GetLegacyDashboardData() => new
{
Charts = new[]
{
new { Name = "Basic Revenue", Type = "Bar", Interactive = false },
new { Name = "Simple Stats", Type = "Table", Interactive = false }
},
Layout = "Classic List",
Theme = "Light Only",
Features = new[] { "Static Reports" }
};
}
Kết quả:
Bạn có thể thay đổi hành vi ứng dụng thông qua cấu hình, mà không cần triển khai lại.
Cách tiếp cận này tiết kiệm thời gian, giảm nguy cơ lỗi khi phát hành, và mang lại cho đội nhóm nhiều quyền kiểm soát hơn đối với chức năng trực tiếp trong sản phẩm.
Yegor Sychev
Senior Software Engineer tại Kaspi.kz
14 tháng 8, 2025