简体   繁体   中英

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}");

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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM