how to handle exception for IHostBuilder CreateHostBuilder

廉价感情. 提交于 2020-08-09 08:14:51

问题


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;
        }
    }
}

回答1:


The application is actually working just fine, I don't think there is any exact way to solve this situation. When you start the application, it's the work of the program class to configure the hosting environment, that includes setting up the server, before calling the Startup class to finish the configuration of the application.

Startup class is responsible for creating the pipeline that handles HTTP request. Which means if any error occurs before the Startup class is configured, the server won't know what do with the error or how to handle the error and hence you get the HTTP 500,

If the error had to be handled after the Startup class has been called, and the HTTP pipeline configured with the Configure method, and you had included the app.UseDeveloperExceptionPage(); Then the correct error message would have been printed back.

The error is generated because you make an HTTP request to the API when building it



来源:https://stackoverflow.com/questions/63037076/how-to-handle-exception-for-ihostbuilder-createhostbuilder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!