A library for strongly typed feature flags in ASP.NET Core.
Package | Version |
---|---|
RimDev.AspNetCore.FeatureFlags |
Install the RimDev.AspNetCore.FeatureFlags NuGet package.
> dotnet add package RimDev.AspNetCore.FeatureFlags
or
PM> Install-Package RimDev.AspNetCore.FeatureFlags
You'll need to wire up Startup.cs
as follows:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using RimDev.AspNetCore.FeatureFlags;
namespace MyApplication
{
public class Startup
{
private readonly FeatureFlagOptions options;
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
options = new FeatureFlagOptions()
.UseCachedSqlFeatureProvider(Configuration.GetConnectionString("localDb"));
}
public void ConfigureServices(IServiceCollection services)
{
services.AddFeatureFlags(options);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseFeatureFlags(options);
// IMPORTANT: Controlling access of the UI / API of this library is the responsibility of the user.
// Apply authentication / authorization around the `UseFeatureFlagsUI` method as needed,
// as this method wires up the various endpoints.
app.UseFeatureFlagsUI(options);
// Or, with ASP.NET Core 3.1:
app.UseRouting();
app.UseEndpoints(endpoints =>
{
// IMPORTANT: Controlling access of the UI / API of this library is the responsibility of the user.
// Apply authentication / authorization around the `UseFeatureFlagsUI` method as needed,
// as this method wires up the various endpoints.
endpoints.MapFeatureFlagsUI(options);
});
}
}
}
Next, create feature flags like this in the ASP.NET Core assembly:
using RimDev.AspNetCore.FeatureFlags;
namespace MyApplication
{
public class MyFeature : Feature
{
// Optional, displays on UI:
public override string Description { get; } = "My feature description.";
}
}
Note: FeatureFlagAssemblies
to scan can also be configured in FeatureFlagOptions
if you'd like to scan assemblies other than Assembly.GetEntryAssembly()
.
Now you can dependency inject any of your feature flags using the standard ASP.NET Core IoC!
public class MyController : Controller
{
private readonly MyFeature myFeature;
public MyController(MyFeature myFeature)
{
this.myFeature = myFeature;
}
// Use myFeature instance here, using myFeature.Value for the on/off toggle value.
}
The UI wired up by UseFeatureFlagsUI
is available by default at /_features
. The UI and API endpoints can be modified in FeatureFlagOptions
if you'd like, too.
MIT License