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?
4 Answer(s)
-
0
Hi @FarReach
The
GetAppConfigurationmethod inStartup.csincludes thesecrets.jsonfile as part of the configuration sources. In this context, it is possible that theMyKeyvalue has been defined asnullwithinsecrets.json. If the value ofMyKeyretrieved from the injected configuration inStartup.csisnull, while other configuration values are accessible, it may indicate thatMyKeyhas been explicitly set tonullinsecrets.json. As is known, thesecrets.jsonfile takes precedence overappsettings.jsonin the configuration hierarchy and can override its values.Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
-
0
Hi @FarReach
My mistake is that the _appConfiguration settings taken from
Startup.csare taken from theAppConfigurationsclass in the *Core project. You can update theBuildConfigurationmethod 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 supportedCopy & paste or drag & drop images (max 30 MB per image) -
0
That worked, thanks for your help!
Markdown is supportedCopy & paste or drag & drop images (max 30 MB per image)
