Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.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通过异常进行筛选_C#_Multithreading_Windows Store Apps_Windows Phone 8.1_Httpclient - Fatal编程技术网

C# 服务器关闭时自定义httpClient通过异常进行筛选

C# 服务器关闭时自定义httpClient通过异常进行筛选,c#,multithreading,windows-store-apps,windows-phone-8.1,httpclient,C#,Multithreading,Windows Store Apps,Windows Phone 8.1,Httpclient,我是windows应用商店应用的新手。我正在尝试向HttpClient添加自定义筛选器,它可以正常工作,但当服务器关闭时,catch块返回null,这会导致系统通过system.AccessViolationException执行。我该如何处理那个案子 public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)

我是windows应用商店应用的新手。我正在尝试向HttpClient添加自定义筛选器,它可以正常工作,但当服务器关闭时,catch块返回null,这会导致系统通过system.AccessViolationException执行。我该如何处理那个案子

  public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)
        {
            HttpResponseMessage response = new HttpResponseMessage();
            return AsyncInfo.Run<HttpResponseMessage, HttpProgress>(async (cancellationToken, progress) =>
            {
                if ((App.userData != null && !string.IsNullOrWhiteSpace(App.userData.Token)) || App.Get_user()) 
                {
                     request.Headers.Add("AuthorizationHeader", App.userData.Token);
                }
                try
                {
                    response = await innerFilter.SendRequestAsync(request);
                    if (response.StatusCode == HttpStatusCode.Forbidden)
                    {
                        ((Frame)Window.Current.Content).Navigate(typeof(LoginPage));
                        return response;
                    }
                    return response;
                }
                catch
                {
                    return null;
                }

            });
        }

如果是服务器停机,那么您可以使用适当的故障代码设置响应变量并返回它,而不是返回null


这就是说,HttpClient.SendRequestAsync的上游调用方需要处理异常,并且不应该因AV而崩溃。文档中明确指出了异常处理的需要。

如何调用SendRequestAsync方法?私有HttpClient\u客户端;var response=wait_client.GetAsyncvalue;然后它将启动,因为它是一个注册到_clientinstancedon的自定义筛选器,无法捕获筛选器中的异常并让它传播