Binateq.FeatureManagement.Flipt 1.2.0

dotnet add package Binateq.FeatureManagement.Flipt --version 1.2.0
NuGet\Install-Package Binateq.FeatureManagement.Flipt -Version 1.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Binateq.FeatureManagement.Flipt" Version="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Binateq.FeatureManagement.Flipt --version 1.2.0
#r "nuget: Binateq.FeatureManagement.Flipt, 1.2.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Binateq.FeatureManagement.Flipt as a Cake Addin
#addin nuget:?package=Binateq.FeatureManagement.Flipt&version=1.2.0

// Install Binateq.FeatureManagement.Flipt as a Cake Tool
#tool nuget:?package=Binateq.FeatureManagement.Flipt&version=1.2.0

Binateq.FeatureManagement.Flipt

The NuGet package Binateq.FeatureManagement.Flipt allows to use the feature-flagging solution Flipt together with the Microsoft.FeatureManagement package.

What for?

The Microsoft.FeatureManagement provides simple and portable standard for .NET applications. It can be extended with feature filters, so you can use both static and dynamic feature flags.

You can add the packet Microsoft.Azure.AppConfiguration.AspNetCore to use dynamic feature flags configured in Microsoft Azure App Configuration service.

But what can you do, if you use Docker/Kubernetes instead of Azure? You can use any feature toggle solution like Flipt that has its own unique methods to work with feature flags.

Or you can use this package that implements feature filters' interface for Flipt gRPC API. In the latter case you can move your application from Azure to Docker and vice versa.

Quick Start

We'll use the standard ASP.NET Core template with Weather Forecast API to check feature flags.

Make the demo Web API application, as it described in the article above. Choose the type Web API and Windows Authentication.

Run it and check, if it works and accessible through the Swagger.

Classic Microsoft.FeatureManagement

Install the Microsoft.FeatureManagement packet:

dotnet add package Microsoft.FeatureManagement --version 2.6.1

Please note that you need to install the version 2.6.1 because something was broken in the 3.0.0..

We all are waiting for updates.

Include feature management support to configuration:

builder.Services.AddFeatureManagement(builder.Configuration.GetSection("FeatureFlags"));

Append new feature flag weather-forecast to the application.json configuration file:

{
  "FeatureFlags": {
    "weather-forecast": false
  }
}

Append the _featureManager to WeatherForecastController and initialize it through the constructor:

private readonly ILogger<WeatherForecastController> _logger;
private readonly IFeatureManager _featureManager;

public WeatherForecastController(ILogger<WeatherForecastController> logger, IFeatureManager featureManager)
{
    _logger = logger;
    _featureManager = featureManager;
}

Rewrite the Get method to use the feature flag:

[HttpGet]
[Produces(typeof(IEnumerable<WeatherForecast>))]
public async Task<IActionResult> Get()
{
    if (await _featureManager.IsEnabledAsync("weather-forecast"))
    {
        return Ok(Enumerable.Range(1, 5)
                            .Select(index => new WeatherForecast
                            {
                                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                                TemperatureC = Random.Shared.Next(-20, 55),
                                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                            }));
    }

    return NotFound();
}

Run the application and call the Get though the Swagger. It should return 404 status. Then make file enabled (in the application.json) and re-run the application. Call the Get again. IT should return 200 status and the collection of forecast.

This static flag requires re-running of re-deploying of applications. Now let's try to use the dynamic flag.

Dynamic flag from Flipt

Install and run Flipt Docker image:

docker run -d -p 8080:8080 -p 9000:9000 -v $HOME/flipt:/var/opt/flipt docker.flipt.io/flipt/flipt:latest

8080 is the port of Flipt Web UI and REST API, and 9000 is the port of gRPC API.

Open http://localhost:8080 after running. Enter the Flags panel and create Boolean flag with name Weather Forecast and key weather-forecast.

Creating feature flag in Flipt UI

Install the package Binateq.FeatureManagement.Flipt:

dotnet add package Binateq.FeatureManagement.Flipt

Append FliptFeatureFilter to composition root:

builder.Services.AddFeatureManagement(builder.Configuration.GetSection("FeatureFlags"))
       .AddFeatureFilter<FliptFeatureFilter>();

Set gRPC API URL and FliptFeatureFilter for the weather-forecast flag in the application.json:

{
  "Flipt": {
    "Url": "http://localhost:9000"
  },
  "FeatureFlags": {
    "weather-forecast": {
      "EnabledFor": [
        {
          "Name": "FliptFeature"
        }
      ]
    }
  }
}

Please note that you need specify the filter FliptFeature in the configuration, although the class is called FliptFeatureFilter.

Run the application and call the Get though the Swagger. It should return 404 status. Enable the flag weather-forecast in the Flipt Web UI and repeat the call. The application should return 200 status and a list of forecasts.

User-specific flag form Flipt

Append FliptPrincipalFeatureFilter to composition root:

builder.Services.AddFeatureManagement(builder.Configuration.GetSection("FeatureFlags"))
       .AddFeatureFilter<FliptFeatureFilter>()
       .AddFeatureFilter<FliptPrincipalFeatureFilter>();

Append UserIdClaim to the Flipt configuration section. Append weather-forecast-principal also.

{
  "Flipt": {
    "Url": "http://localhost:9000",
    "UserIdClaim": "ClaimTypes.PrimarySid"
  },
  "FeatureFlags": {
    "weather-forecast": {
      "EnabledFor": [
        {
          "Name": "FliptFeature"
        }
      ]
    },
    "weather-forecast-principal": {
      "EnabledFor": [
        {
          "Name": "FliptPrincipal"
        }
      ]
    }
  }
}

Append new GetPrincipal method to use the feature flag:

[HttpGet("principal")]
[Produces(typeof(IEnumerable<WeatherForecast>))]
[Authorize]
public async Task<IActionResult> GetPrincipal()
{
    if (await _featureManager.IsEnabledAsync("weather-forecast-forecast", HttpContext.User))
    {
        return Ok(Enumerable.Range(1, 5)
                            .Select(index => new WeatherForecast
                            {
                                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                                TemperatureC = Random.Shared.Next(-20, 55),
                                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                            }));
    }

    return NotFound();
}

Run the Demo application and check GET /weather-forecast/principal endpoint. It should return Not Found. Now run in command line:

whoami /user

You'll see SID of your user account. Open Flipt UI, and create new segment Specified Users with the key specified-users. Set Match Type to Any.

Append the constraint UserId with your account's SID as the value. Choose == as the operator.

Create new Boolean flag Weather Forecast Principal with the key weather-forecast-principal. Append the Rollout of the Segment type. Choose the segment specified-users, and set the Value field to true.

Check GET /weather-forecast/principal endpoint again. It should return a list of forecasts and Ok status.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.2.0 202 11/27/2023
1.0.0 86 11/23/2023

Implement FliptPrincipalFeatureFilter.