Nov 18 2024
dotnet add package Microsoft.FeatureManagement.AspNetCore
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore
using Microsoft.FeatureManagement;
var builder = WebApplication.CreateBuilder(args);
// Add Azure App Configuration
builder.Configuration.AddAzureAppConfiguration(options =>
options.Connect("<Your Connection String>")
.UseFeatureFlags());
// Add Feature Management services
builder.Services.AddFeatureManagement();
var app = builder.Build();
// Use Feature Management middleware
app.UseAzureAppConfiguration();
app.MapGet("/", async context =>
{
var featureManager = context.RequestServices.GetRequiredService<IFeatureManager>();
if (await featureManager.IsEnabledAsync("FeatureX"))
{
await context.Response.WriteAsync("FeatureX is enabled!");
}
else
{
await context.Response.WriteAsync("FeatureX is disabled.");
}
});
app.Run();
@inject IFeatureManager FeatureManager
@if (await FeatureManager.IsEnabledAsync("DarkMode"))
{
<link href="dark-theme.css" rel="stylesheet" />
}
else
{
<link href="light-theme.css" rel="stylesheet" />
}
using System.Threading.Tasks;
public interface IFeatureToggleService
{
Task<bool> IsFeatureEnabledAsync(string featureName);
}
using Microsoft.FeatureManagement;
public class FeatureToggleService : IFeatureToggleService
{
private readonly IFeatureManager _featureManager;
public FeatureToggleService(IFeatureManager featureManager)
{
_featureManager = featureManager;
}
public async Task<bool> IsFeatureEnabledAsync(string featureName)
{
return await _featureManager.IsEnabledAsync(featureName);
}
}
builder.Services.AddSingleton<IFeatureToggleService, FeatureToggleService>();
public class MyBusinessService
{
private readonly IFeatureToggleService _featureToggleService;
public MyBusinessService(IFeatureToggleService featureToggleService)
{
_featureToggleService = featureToggleService;
}
public async Task<string> GetFeatureDependentMessageAsync()
{
//Note: Move magic string "FeatureX" in some constant.
if (await _featureToggleService.IsFeatureEnabledAsync("FeatureX"))
{
return "FeatureX is enabled! Business logic for enabled feature.";
}
else
{
return "FeatureX is disabled. Business logic for disabled feature.";
}
}
}
using Moq;
using Xunit;
public class FeatureToggleServiceTests
{
[Fact]
public async Task IsFeatureEnabledAsync_Returns_True_When_Feature_Is_Enabled()
{
// Arrange
var featureManagerMock = new Mock<IFeatureManager>();
featureManagerMock.Setup(fm => fm.IsEnabledAsync("FeatureX")).ReturnsAsync(true);
var service = new FeatureToggleService(featureManagerMock.Object);
// Act
var result = await service.IsFeatureEnabledAsync("FeatureX");
// Assert
Assert.True(result);
}
}
1. Design Patterns that Deliver
This isn’t just another design patterns book. Dive into real-world examples and practical solutions to real problems in real applications.Check out it here.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Join 14,250+ subscribers to improve your .NET Knowledge.
Subscribe to the TheCodeMan.net and be among the 14,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.