Base solution for your next web application
Open Closed

Integrating Azure App Configuration #12447


User avatar
0
FarReach created

We are trying to integrate Azure App Configuration for our angular/c# app and seem to be running into a timing issue for when configs are available. We updated our Program.cs as shown below: public static IWebHostBuilder CreateWebHostBuilder(string[] args) { return new WebHostBuilder() .ConfigureAppConfiguration((context, config) => { var settings = config.AddJsonFile("appsettings.json", false, true) .AddEnvironmentVariables() .Build(); config.AddAzureAppConfiguration(settings["AppConfig:ConnectionString"]); }) .UseKestrel(opt => { opt.AddServerHeader = false; opt.Limits.MaxRequestBufferSize = 32 * 1024; opt.Limits.MaxRequestLineSize = 32 * 1024; }) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIIS() .UseIISIntegration() .UseApplicationInsights() .UseStartup<Startup>(); }

However in the Startup.cs when we try to pull a config value using _appConfiguration["MyKey"] it is always null.

But if I inject IConfiguration to any of our app service classes it is available: public MyAppService( IConfiguration config ) { var test = config["MyKey"]; }

Is there a way to make sure the azure app configurations are ingested so they are available in the Startup.cs since 90% of the configuration is setup there?

Markdown is supported
Copy & paste or drag & drop images (max 30 MB per image)

4 Answer(s)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @FarReach

    The GetAppConfiguration method in Startup.cs includes the secrets.json file as part of the configuration sources. In this context, it is possible that the MyKey value has been defined as null within secrets.json. If the value of MyKey retrieved from the injected configuration in Startup.cs is null, while other configuration values are accessible, it may indicate that MyKey has been explicitly set to null in secrets.json. As is known, the secrets.json file takes precedence over appsettings.json in the configuration hierarchy and can override its values.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    FarReach created

    We are not using the secrets.json file, we are storing the settings in azure and ingesting them.

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    oguzhanagir created
    Support Team

    Hi @FarReach

    My mistake is that the _appConfiguration settings taken from Startup.cs are taken from the AppConfigurations class in the *Core project. You can update the BuildConfiguration method in this class as follows. I think this will solve your problem.

    private static IConfigurationRoot BuildConfiguration(string path, string environmentName = null,
        bool addUserSecrets = false)
    {
        //User Secrets Config
    
        // Azure App Configuration 
        var tempConfig = builder.Build();
        var azureAppConfigConnectionString = tempConfig["AzureAppConfig:ConnectionString"];
        if (!azureAppConfigConnectionString.IsNullOrEmpty())
        {
            builder.AddAzureAppConfiguration(azureAppConfigConnectionString);
        }
    
        //Azure Key Vault Config
    
        return builder.Build();
    }
    

    Or With Feature Flags

    var tempConfig = builder.Build();
    var azureAppConfigConnectionString = tempConfig["AzureAppConfig:ConnectionString"];
    if (!azureAppConfigConnectionString.IsNullOrWhiteSpace())
    {
        builder.AddAzureAppConfiguration(options =>
        {
            options.Connect(azureAppConfigConnectionString)
                   .UseFeatureFlags();
        });
    }
    
    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)
  • User Avatar
    0
    FarReach created

    That worked, thanks for your help!

    Markdown is supported
    Copy & paste or drag & drop images (max 30 MB per image)