如何处理IHostBuilder CreateHostBuilder的异常 [英] how to handle exception for IHostBuilder CreateHostBuilder

查看:1424
本文介绍了如何处理IHostBuilder CreateHostBuilder的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为模拟错误,我提供了错误的天蓝色密钥存储库地址.用下面的代码;我尝试了所有可能的方法来尝试/捕获异常,但是在应用启动时仍然出现错误.

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?

我有ASP.NET Core 3.1 Web API应用程序.

I have ASP.NET Core 3.1 web API application.

HTTP错误500.30-ANCM进程内启动失败

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:未知此类主机."

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

推荐答案

该应用程序实际上运行正常,我认为没有任何确切的方法可以解决这种情况. 启动应用程序时,程序类的工作是配置托管环境,包括设置服务器,然后调用Startup类完成应用程序的配置.

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.

启动类负责创建处理HTTP请求的管道.这意味着如果在配置Startup类之前发生任何错误,服务器将不知道如何处理该错误或如何处理该错误,因此您将获得HTTP 500,

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,

如果必须在调用Startup类并使用Configure方法配置了HTTP管道之后处理该错误,并且您已经包含了 app.UseDeveloperExceptionPage(); 这样,正确的错误消息就会被打印回去.

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.

生成错误是因为您在构建API时向API发出了HTTP请求

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

这篇关于如何处理IHostBuilder CreateHostBuilder的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆