Skip to content

Custom feature flag using Microsoft.FeatureManagement

License

Notifications You must be signed in to change notification settings

dianper/feature-toggle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CircleCI

On/Off Feature Toggle Core

The features can be enabled by

  • Cookies
  • Headers
  • QueryString

How to use

Toggle Declaration

// appsettings.json
"FeatureManagement": {
  "FeatureA": false,
  "FeatureB": false,
  "FeatureC": {
    "EnabledFor": [
      {
        "Name": "Cookies"
      },
      {
        "Name": "QueryString"
      }
    ]
  },
  "FeatureD": {
    "EnabledFor": [
      {
        "Name": "Cookies"
      }
    ]
  }
}

Service Registration

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpContextAccessor();
    services.AddMvc();
    services.AddFeatureManagement()
        .AddFeatureFilter<CookiesFilter>()
        .AddFeatureFilter<HeadersFilter>()
        .AddFeatureFilter<QueryStringFilter>();
}

Dependency Injection

private readonly IFeatureManager featureManager;

public DummyClass(IFeatureManager featureManager)
{
    this.featureManager = featureManager;
}

Check

private const string FeatureName = "FeatureC";

public async Task DummyMethod()
{
    if(await this.featureManager.IsEnabledAsync(FeatureName))
    {
      // Do something
    }
}

Build

dotnet build

Test

dotnet test

Running

Turn on by querystring

dotnet run --project .\src\Dianper.FeatureToggle\Dianper.FeatureToggle.Sample

http://localhost:55715/api/values?FeatureC=true

Reference

Microsoft.FeatureManagement