muench-develops.FeatureToggler
1.0.3
dotnet add package muench-develops.FeatureToggler --version 1.0.3
NuGet\Install-Package muench-develops.FeatureToggler -Version 1.0.3
<PackageReference Include="muench-develops.FeatureToggler" Version="1.0.3" />
paket add muench-develops.FeatureToggler --version 1.0.3
#r "nuget: muench-develops.FeatureToggler, 1.0.3"
// Install muench-develops.FeatureToggler as a Cake Addin #addin nuget:?package=muench-develops.FeatureToggler&version=1.0.3 // Install muench-develops.FeatureToggler as a Cake Tool #tool nuget:?package=muench-develops.FeatureToggler&version=1.0.3
muench-develops.FeatureToggler
muench-develops.FeatureToggler
is a lightweight and extensible library for managing feature flags in .NET applications. It allows you to dynamically enable or disable features using multiple providers.
Features
- Multiple Providers: In-Memory, JSON, Environment Variables.
- ASP.NET Core Integration: Middleware for feature-based request handling.
- Extensible: Easily add custom providers.
- Thread-Safe: Safe for concurrent updates and queries.
Installation
Install via NuGet:
dotnet add package muench-develops.FeatureToggler
Quick Start
1. Register Feature Flags
In-Memory Provider
For quick development and testing:
builder.Services.AddFeatureFlags(
new InMemoryFeatureFlagProvider(
new[]
{
new FeatureFlag("EnableNewUI", true, "Enables the new UI"),
new FeatureFlag("BetaFeature", false, "Beta feature toggle")
})
);
JSON Provider
To manage feature flags via a JSON file, use JsonFeatureFlagProvider
:
- Create a JSON file, e.g.,
featureflags.json
:
[
{ "Key": "EnableNewUI", "IsEnabled": true, "Description": "Enables the new UI" },
{ "Key": "BetaFeature", "IsEnabled": false, "Description": "Beta feature toggle" }
]
- Register the provider in
Program.cs
:
builder.Services.AddFeatureFlags(
new JsonFeatureFlagProvider("path/to/featureflags.json", new FileReader())
);
Environment Variable Provider
Use environment variables to control feature flags dynamically:
- Set environment variables:
$env:ENABLE_NEW_UI = "true"
$env:BETA_FEATURE = "false"
- Register the provider in Program.cs:
builder.Services.AddFeatureFlags(
new EnvironmentVariableFeatureFlagProvider(new Dictionary<string, string>
{
{ "EnableNewUI", "ENABLE_NEW_UI" },
{ "BetaFeature", "BETA_FEATURE" }
})
);
Configuration Provider
The ConfigurationFeatureFlagProvider
enables you to manage feature toggles directly from the .NET IConfiguration
system, which can include sources like appsettings.json
, environment variables, or other configuration providers.
{
"FeatureToggles": {
"EnableNewUI": {
"IsEnabled": true,
"Description": "Enables the new UI"
},
"BetaFeature": {
"IsEnabled": false,
"Description": "This is a beta feature toggle"
}
}
}
using FeatureToggler.Providers;
var builder = WebApplication.CreateBuilder(args);
// Register Feature Flags with the Configuration Provider
builder.Services.AddFeatureFlags(
new ConfigurationFeatureFlagProvider(builder.Configuration)
);
var app = builder.Build();
2a. Use Middleware
Execute actions based on feature flag states:
app.UseFeatureFlag("EnableNewUI",
onEnabled: () => Console.WriteLine("New UI is enabled!"),
onDisabled: () => Console.WriteLine("New UI is disabled!"));
2b. Use Minimal API
app.MapGet("/", (FeatureFlagManager manaer) => manager.IsEnabled("EnableNewUI")
? Results.Ok("Welcome to the new UI!")
: Results.Ok("Welcome to the old UI!"));
3. Query Flags
Check or update feature flags dynamically:
var manager = app.Services.GetRequiredService<FeatureFlagManager>();
if (manager.IsEnabled("EnableNewUI"))
{
Console.WriteLine("New UI is enabled!");
}
License
This project is licensed under the Apache License. See the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- Microsoft.AspNetCore.Http.Abstractions (>= 2.2.0)
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.Configuration.Binder (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 9.0.0)
- System.Text.Encodings.Web (>= 4.7.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.