ASP.NETCORE use feature flags inside ConfigureServices

Viewed 751

I'm using Feature Flags as described here. It works alright, I'm just wondering if the following is really the best way to use them if I want to switch between different service implementations inside ConfigureServices()?

I have the following so far:

using Microsoft.FeatureManagement;

public void ConfigureServices(IServiceCollection services)
{
    services.AddFeatureManagement();

    var sp = services.BuildServiceProvider();
    var featureManager = sp.GetService<IFeatureManager>();

    if (featureManager.IsEnabledAsync("UseServiceB").Result)
    {
        services.AddHostedService<MyServiceB>();
    }
    else
    {
        services.AddHostedService<MyServiceA>();
    }
}

Especially since IFeatureManager only provides an async method to check for a feature, this doesn't feel quite right?!

0 Answers
Related