.NET Feature Management Powered by a Rest API

Ben
6 min readJul 18, 2023
Photo by Yancy Min on Unsplash

Microsoft has a pretty good feature management system available to use in most .NET applications. It’s based around IConfiguration and it’s quick to set up with a JSON file to manage feature flags.

It has some pretty nice features like setting a feature to be enabled based on a time window, it can be configured based on a percentage, and then you have the basic on/off switch mode.

Now, I don’t really have a need for anything fancy. I would, however, like to be able to pull in my feature flags from a database. I need to go through a Rest API in order to access that data due to some project restrictions. So let me show you what I did to get that working.

Just want the code?

Here is a GitHub repository with the code from this article.

https://github.com/BenBurge/Medium.Articles.FeatureFlags

The Setup

  • Using .NET 7
  • Using Microsoft.EntityFrameworkCore
  • Postgres DB and the accompanying Nuget package Npgsql.EntityFrameworkCore.PostgreSQL
  • Using the Microsoft.FeatureManagement Nuget package
  • A basic API project
  • A basic console application

Rest API Endpoint

So the first step was creating a model to store some information about my feature flag. I created an extremely basic object as shown here.

public class FeatureFlag
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public bool IsEnabled { get; set; }
}

Next, I setup a basic Entity Framework DBContext

  public class FeatureFlagDbContext : DbContext
{
public FeatureFlagDbContext(DbContextOptions<FeatureFlagDbContext> options) :
base(options)
{ }

public DbSet<FeatureFlag> FeatureFlags => Set<FeatureFlag>();
}

I like to use Postgres, but you can use any provider you’d like. I setup the required services in my program.cs

builder.Services.AddDbContextFactory<FeatureFlagDbContext>(options =>
{…

--

--