0

我们有 FeatureFlag: IsServiceNeeded,在 Azure 应用配置的功能管理器中没有设置标签。我们通过设置缓存间隔在内置方法 AddAzureAppConfiguration 中使用。

我们正在使用 .net core 3.1 web api 和 Azure 应用配置的功能管理器。

我们在应用程序初始化期间启用了 IsServiceNeeded,几个小时后,我们禁用了 IsServiceNeed。我们等了一整天,但看不出有什么区别,因为下面返回的是 true 而不是 false。由于我们在 program.cs 文件中配置它的方式,我们希望它每 3 分钟更新一次。

await _featureManager.IsEnabledAsync("IsServiceNeeded")

如果您在下面看到任何奇怪的地方,请告诉我。提前致谢,

这是我们正在使用的代码片段。

程序.cs 文件

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureAppConfiguration((context, config) =>
            {
                var configurationRoot = config.Build();
                var appConfigConString = configurationRoot["AppConfigConnectionString"];

                config.AddAzureAppConfiguration(options => options.Connect(appConfigConString).UseFeatureFlags(featureFlagOptions => {
                    **featureFlagOptions.CacheExpirationInterval = TimeSpan.FromMinutes(3);**
                }));
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}

启动.cs 文件

public class Startup
{
    public IConfiguration Configuration { get; }
    public string ContentRootPath { get; set; }
    
    public Startup(IWebHostEnvironment env, IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddHsts(options =>
        {
            options.Preload = true;
            options.IncludeSubDomains = true;
            options.MaxAge = TimeSpan.FromDays(365);
        });

        var conf = Configuration.GetSection("AppSettings").Get<Config>();

        services.Configure<Config>(Configuration.GetSection("AppSettings"));

        services.AddSingleton<IAppSettings>(c => conf);**

        services.AddScoped<IProcessHandler, ProcessHandler>();
        **services.AddFeatureManagement();**
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseHsts();
        app.UseHttpsRedirection();
        app.UseHttpStatusCodeExceptionMiddleware();

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseAuthentication();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }

        private static void LoadMediatorHandlers(IServiceCollection services)
        {
            foreach (var assembly in Assembly
                                     .GetEntryAssembly()
                                     .GetReferencedAssemblies()
                                     .Select(Assembly.Load)
                                     .Where(name => (name.FullName.Contains("Queries") || name.FullName.Contains("Commands"))))
            {
                services.AddMediatR(assembly);
            }
            services.AddMediatR(typeof(Startup));
            services.AddScoped<IMediator, Mediator>();
        }   
    }

特征标志的应用:

public class ProcessHandler : IProcessHandler
{
    private readonly IFeatureManager _featureManager;

    public ProcessHandler(IFeatureManager featureManager)
    {
        _featureManager = featureManager;
    }

    public async Task<ClassA> ProcessXyz()
    {
        if (`await _featureManager.IsEnabledAsync("IsServiceNeeded")`)
        {
            return new ClassA();
        }
        
        return null;
    }   
}

请注意:我刚刚添加了所需的代码并替换了实际名称以解决安全问题。

提前致谢

4

0 回答 0