简体   繁体   English

Azure 启动时的功能管理

[英]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 =>我想使用 Azure 功能管理来驱动是否在启动期间将服务添加到依赖.ConfigureServices(hostcontext, services =>

The only way I can find to do this is to call.BuildServiceProvider to get the IFeatureManagement.我能找到的唯一方法是调用 BuildServiceProvider 来获取 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?或者也许通过使用可以获取其他配置值的 IConfiguration 接口?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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