Home > Blockchain >  Application doesn't load azure app config feature flag value but loads config values
Application doesn't load azure app config feature flag value but loads config values

Time:10-19

I have a Blazor app that I want to use Azure app config.

I've successfully setup getting config values, however it doesn't find my feature flag.

When I inject an IConfiguration and query the value of a config value, like so Configuration["value"] the proper data is retrieved.
But when I try and check if my only feature flag is enabled (and it is) the result is always false, which I imagine is because the app can't find the feature flag, not because it's reading the value wrong (passing the name of a non-existant feature flag causes the method to return "false").

Startup.cs:

using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Web;
using Microsoft.Identity.Web.UI;
using Microsoft.FeatureManagement;

namespace OreNoAppu
{
    public class Startup
    {
        public IConfiguration Configuration { get; }

        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
            services.AddControllersWithViews().AddMicrosoftIdentityUI();

            // By default, all incoming requests will be authorized according to the default policy
            services.AddAuthorization(options => options.FallbackPolicy = options.DefaultPolicy);

            services.AddRazorPages();
            services.AddServerSideBlazor().AddMicrosoftIdentityConsentHandler();
            services.AddFeatureManagement();
            services.AddAzureAppConfiguration();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days.You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();
            app.UseAzureAppConfiguration();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapBlazorHub();
                endpoints.MapFallbackToPage("/_Host");
            });
        }
    }
}

Program.cs:

using Azure.Core;
using Azure.Identity;
using Azure.Security.KeyVault.Secrets;
using BoomiLogReader.Utility;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;

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

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                    webBuilder.ConfigureAppConfiguration(config =>
                    {
                        var connection = KeyVaultReader.GetSecretValue("AzureAppConfigConnStr");
                        config.AddAzureAppConfiguration(options =>
                            options.Connect(connection).UseFeatureFlags());
                    }).UseStartup<Startup>());

        // Initialises KeyVaultReader, which is used to retrieve secrets from Azure Key Vault.
        public static void InitKeyVault()
        {
            // N'importe quoi, access to KeyVault and getting of the conn string works fine.
        }
    }
}

Code that should be getting the flag:

@using Microsoft.FeatureManagement
@inject IFeatureManager featureManager

@if (featureEnabled)
{
    <h1>Yes</h1>
}

@code
{
    bool featureEnabled = true;

    protected override async Task OnInitializedAsync()
    {
        featureEnabled = featureManager.IsEnabledAsync("OreNoFlaggu").Result;
    }
}

Debugging, I see that the method call "IsEnabledAsync" returns "false", but there is a "OreNoFlaggu" feature flag in my app config, that the application successfully connects and retrieves config values from, and said flag is enabled.

Any idea why it isn't working?

CodePudding user response:

@Tessaract, does your feature flag happen to have a label in your App Config store. If so, please don't forget to pass the label when calling UseFeatureFlags() via FeatureFlagOptions.Label.

  • Related