I have configured my Azure App to use key vault references in configuration settings. Everything works fine in general, but there is a problem when I want a setting to have a default value in case it's missing from the key vault.
Here is my simplified code example:
public class MySettings
{
public bool DoSomethingSpecial { get; set; }
public string SomeStringSetting { get; set; }
}
Startup:
serviceCollection.Configure<MySettings>(x =>
{
configuration.Bind("MyApp:MySettings", x);
});
Azure App Configuration setting:
MyApp__MySettings__DoSomethingSpecial
@Microsoft.KeyVault(SecretUri=https://myapp.vault.azure.net/secrets/MyApp--MySettings--DoSomethingSpecial)
If I don't add DoSomethingSpecial = false
to the key vault, the app throws an error on startup:
Failed to convert configuration value at 'MyApp:MySettings:DoSomethingSpecial' to type 'System.Boolean'. @Microsoft.KeyVault(SecretUri=https://andromeda-keyvault-dev.vault.azure.net/secrets/MyApp--MySettings--DoSomethingSpecial) is not a valid value for Boolean. String '@Microsoft.KeyVault(SecretUri=https://andromeda-keyvault-dev.vault.azure.net/secrets/MyApp--MySettings--DoSomethingSpecial)' was not recognized as a valid Boolean.
It means Azure treats missing key vault references as raw literal strings.
I don't have that setting mentioned in appsettings.json
- it's not needed there.
My Program.cs
is pretty basic old-style .NET Core app launcher:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddEnvironmentVariables();
})
.UseStartup<Startup>();
}
Is there any nice way to have a default false
value in case the key vault value is missing?
Currently, the only (ugly) workaround that comes to my mind is to try .. catch
around every setting field of MySettings in the serviceCollection.Configure<MySettings>
.