我已将我的 Azure 应用程序配置为在配置设置中使用密钥保管库引用。一般情况下一切正常,但是当我希望设置具有默认值以防它从密钥库中丢失时出现问题。
这是我的简化代码示例:
public class MySettings
{
public bool DoSomethingSpecial { get; set; }
public string SomeStringSetting { get; set; }
}
启动:
serviceCollection.Configure<MySettings>(x =>
{
configuration.Bind("MyApp:MySettings", x);
});
Azure 应用程序配置设置:
MyApp__MySettings__DoSomethingSpecial
@Microsoft.KeyVault(SecretUri=https://myapp.vault.azure.net/secrets/MyApp--MySettings--DoSomethingSpecial)
如果我不将
DoSomethingSpecial = false
添加到密钥库,应用程序会在启动时抛出错误:
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.
这意味着 Azure 将丢失的密钥保管库引用视为原始文字字符串。
是否有任何好的方法来获得默认
false
值以防密钥保管库值丢失?
目前,我想到的唯一(丑陋的)解决方法是
try .. catch
围绕serviceCollection.Configure<MySettings>
中MySettings的每个设置字段。