To simulate the error, I gave the wrong azure key vault address. With the below code; I tried all the possible ways to try/catch the exception, but still I get an error when the app is start.
How do I handle this exception so the application does NOT throw the error during startup?
I have ASP.NET Core 3.1 web API application.
HTTP Error 500.30 - ANCM In-Process Start Failure
The actual reason for the error is that I put wrong key vault address,
System.Net.Http.HttpRequestException: 'No such host is known.'
public class Program
{
public static void Main(string[] args)
{
try
{
CreateHostBuilder(args).Build().Run();
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((context, config) =>
{
try
{
var keyVaultClient = KeyVaultClient();
if (keyVaultClient != null)
config.AddAzureKeyVault("https://testkeyvault07021.vault.azure.net", keyVaultClient,
new DefaultKeyVaultSecretManager());
}
catch (Exception exception)
{
Console.WriteLine(exception);
}
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
private static KeyVaultClient KeyVaultClient()
{
try
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var keyVaultClient =
new KeyVaultClient(
new KeyVaultClient.AuthenticationCallback(azureServiceTokenProvider.KeyVaultTokenCallback));
return keyVaultClient;
}
catch (Exception exception)
{
Console.WriteLine(exception);
return null;
}
}
}