Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
...
services.Configure<Dictionary<string, bool>>(Configuration.GetSection("FeatureFlags"));
services.AddSingleton<IFeatureFlagProvider, ConfigFileProvider.ConfigFileFeatureFlagProvider>();
services.AddSingleton<IFeatureFlagService, FeatureFlagService>();
...
}
To add a new feature flag we have to add a new entry in the config file. In this example we add a feature flag with the key "US101_AddNewIntegration"
appsettings.json:
{
"FeatureFlags": {
"US101_ReturnNewValues": true,
}
}
Note that the sections name must be the same as the one provided in Startup.cs
Add a new class that extends FeatureFlag.cs
public class FFReturnNewValues : FeatureFlag
{
public override string Key => "US101_ReturnNewValues";
}
A IFeatureFlagService is needed to check if a feature flag is active. The below example shows how to check if the feature flag FFReturnNewValues is active.
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
namespace SolidifyLabs.FeatureFlags.Demo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
private readonly IFeatureFlagService _featureFlagService;
public ValuesController(IFeatureFlagService featureFlagService)
{
_featureFlagService = featureFlagService;
}
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
if (_featureFlagService.Is<MyFlag>().Enabled)
{
return new string[] {"wow", "such feature"};
}
return new string[] { "value1", "value2" };
}
}
}