Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# httpClient.getAsync异常绕过Try/Catch块_C#_Exception Handling_Async Await - Fatal编程技术网

C# httpClient.getAsync异常绕过Try/Catch块

C# httpClient.getAsync异常绕过Try/Catch块,c#,exception-handling,async-await,C#,Exception Handling,Async Await,我正在尝试运行到远程Web服务的等待异步连接。URL是可配置的,如果无效,我将得到一个空引用异常,该异常将忽略Try/Catch块并使整个IIS工作进程崩溃 public static void SendMessage(FileInfo FileToSend, string RecipientName, string RecipientNumber){ { //Connection Info retrieved from DB string url = "http://NotAR

我正在尝试运行到远程Web服务的等待异步连接。URL是可配置的,如果无效,我将得到一个空引用异常,该异常将忽略Try/Catch块并使整个IIS工作进程崩溃

public static void SendMessage(FileInfo FileToSend, string RecipientName, string RecipientNumber){
{
    //Connection Info retrieved from DB
    string url = "http://NotARealUrl.com/";
    string username = "NotARealUN";
    string password = "NotARealPW";

    //not Awaiting this because all logging in event of job failure is handled by the job itself. There is no reason to wait for completion
    Send (FileToSend, url, username, password, RecipientName, RecipientNumber)
}

public static async Task Send(FileInfo FileToSend, string url, string username, string password, string sentByID, string faxRecipient, string faxNumber)
{
    var handler = new HttpClientHandler
    {
        Credentials = new NetworkCredential(username, password), 
        PreAuthenticate = true
    };

    var client = new HttpClient(handler);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    try
    {
        var response = await client.GetAsync(url); //Exception is throw when I step past this point
        response.EnsureSuccessStatusCode();

        var contentAsString = await response.Content.ReadAsStringAsync();
        var root = JsonConvert.DeserializeObject<FaxingAPIRoot>(contentAsString);

        //Code continues, using 'root' in order to send the message in accordance with the Faxing API I'm using
    }
    catch (Exception e)
    {
        //Graceful Error Handling 
    }
}
代码引发以下异常,该异常绕过Catch块并立即使主机进程崩溃,无论是Visual Studio的IIS Express还是IIS工作进程

public static void SendMessage(FileInfo FileToSend, string RecipientName, string RecipientNumber){
{
    //Connection Info retrieved from DB
    string url = "http://NotARealUrl.com/";
    string username = "NotARealUN";
    string password = "NotARealPW";

    //not Awaiting this because all logging in event of job failure is handled by the job itself. There is no reason to wait for completion
    Send (FileToSend, url, username, password, RecipientName, RecipientNumber)
}

public static async Task Send(FileInfo FileToSend, string url, string username, string password, string sentByID, string faxRecipient, string faxNumber)
{
    var handler = new HttpClientHandler
    {
        Credentials = new NetworkCredential(username, password), 
        PreAuthenticate = true
    };

    var client = new HttpClient(handler);
    client.DefaultRequestHeaders.Accept.Clear();
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    try
    {
        var response = await client.GetAsync(url); //Exception is throw when I step past this point
        response.EnsureSuccessStatusCode();

        var contentAsString = await response.Content.ReadAsStringAsync();
        var root = JsonConvert.DeserializeObject<FaxingAPIRoot>(contentAsString);

        //Code continues, using 'root' in order to send the message in accordance with the Faxing API I'm using
    }
    catch (Exception e)
    {
        //Graceful Error Handling 
    }
}
“System.NullReferenceException”类型的未处理异常 发生在mscorlib.dll中

其他信息:对象引用未设置为 反对

有一个调用堆栈

    mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.ThrowAsyncIfNecessary.AnonymousMethod__18_0(object s) 
    mscorlib.dll!System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(object state)  
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   
    mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)   
    mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()  
    mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch()    
    mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() 
    [Native to Managed Transition]  

我已经验证了传入的URL不为null,HttpClient不为null。有没有一种方法可以强制捕获此异常,或者在尝试调用该URL之前检查该URL的有效性来抢占错误?

GetAsync部分的调用代码是什么?这个错误看起来好像你在某个时候没有等待。请检查您的整个呼叫链。@ckuri肯定有一点我没有等待。但是,所有错误处理和错误日志记录都是等待部分的内部内容。我试着把它当作一场火灾来触发,然后忘记整个功能。我将展开代码部分来展示我是如何做到这一点的(也许我做错了)@abatishchev不,我没有。上面的评论解释了原因。这是一种用火而忘却的方法。我可能处理得不正确,但我不想等待方法完成…只要启动它并继续。然后看好了…这显然修复了它。现在……有人能提供一些见解来解释为什么这样解决了这个问题吗?