Azure Feature Management on startup

1k Views Asked by At

I'm wanting to use Azure feature management to drive whether services are added to dependencies during startup .ConfigureServices(hostcontext, services =>

The only way I can find to do this is to call .BuildServiceProvider to get the IFeatureManagement.

var featureManager = services.BuildServiceProvider().GetRequiredService<IFeatureManager>();
if (featureManager.IsEnabledAsync(Features.MY_FEATURE).GetAwaiter().GetResult())
{
    services.AddFeatureDependencies();
}

There has got to be a better way to do this. Is there a service extension I'm missing somewhere? Something like below?

services.IfFeatureEnabled(Features.MY_FEATURE) {
    services.AddFeatureDependencies();
}

Or maybe by using the IConfiguration interface which can get other configuration values?

hostContext.Configuration.GetValue<bool>($"FeatureManager:{FeatureManagement.MY_FEATURE}");
1

There are 1 best solutions below

0
On BEST ANSWER

I found usefull to create extension method for this:

public static class ConfigurationExtensions
{
    public static bool IsFeatureEnabled(this IConfiguration configuration, string feature)
    {
        var featureServices = new ServiceCollection();
        featureServices.AddFeatureManagement(configuration);
        using var provider = featureServices.BuildServiceProvider();
        var manager = provider.GetRequiredService<IFeatureManager>();

        return manager.IsEnabledAsync(feature).GetAwaiter().GetResult();
    }
}