Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure Feature Management on startup

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}");
like image 589
Ryan Langton Avatar asked Nov 01 '25 23:11

Ryan Langton


1 Answers

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();
    }
}
like image 51
Jan Muncinsky Avatar answered Nov 03 '25 13:11

Jan Muncinsky