PostAsJsonAsync .Net 4.0的超时问题

我正在通过.net客户端桌面应用程序正在使用的dll文件调用网络api。 Dll文件写在.Net 4.0中。

有时我在这条线上经常出错。

var postResponse = client.PostAsJsonAsync(path,Input).Result;

出现错误消息后,我检查了IIS和Web服务日志文件。根据日志文件,上面的代码行没有进入Web服务,因为Web方法将所有活动记录在其自己的日志文件中。

有没有办法避免超时错误。

DLL文件代码

 private T CallService<T>(string path)
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = EndpointAddress;
            client.DefaultRequestHeaders.accept.Clear();
            client.DefaultRequestHeaders.accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout = TimeSpan.FromSeconds(30);

            var response = client.PostAsJsonAsync(path,Input).Result;

            if (response.IsSuccessful)
            {
                var serviceResponse = response.Content.ReadAsAsync<T>().Result;

               // return service response;
            } 
            else
            {
               // return default (T)
            }
        }
    }

网络服务方法

       [HttpPost]
        public CusResponse getcustomer(CustomerOperationInput input)
        {


        }

这是超时时的标头数据

{StatusCode: 500,ReasonPhrase: 'Internal Server Error',Version: 1.1,Content: System.Net.Http.StreamContent,Headers:
{
  Pragma: no-cache
  Connection: close
  Cache-Control: no-cache
  Date: Wed,06 Nov 2019 11:06:45 GMT
  Server: microsoft-IIS/7.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 36
  Content-Type: application/json; charset=utf-8
  Expires: -1
}}

IIS跟踪文件中的错误

<EventData>
  <Data Name="ContextId">{00000000-0000-0000-230F-008000000019}</Data>
  <Data Name="ModuleName">ManagedPipelineHandler</Data>
  <Data Name="Notification">128</Data>
  <Data Name="HttpStatus">500</Data>
  <Data Name="HttpReason">Internal Server Error</Data>
  <Data Name="HttpSubStatus">0</Data>
  <Data Name="ErrorCode">0</Data>
  <Data Name="ConfigExceptionInfo"></Data>
 </EventData>

PostAsJsonAsync .Net 4.0的超时问题

PostAsJsonAsync .Net 4.0的超时问题

swf1014 回答:PostAsJsonAsync .Net 4.0的超时问题

这不是超时。超时表示:“因为没有回应,我放弃了等待。”

但是,您从服务器获得了响应。您完全拥有“标题数据”这一事实表明了这一点。回复为500 Internal Server Error。当引发未处理的异常时,这将在ASP.NET中自动发生,但也可以显式发送它。无论哪种方式,都意味着发生了不好的事情。

Content-Type标头设置为application/json,因此响应的正文中可能会有一条错误消息,您可以使用该错误消息来找出问题所在。

因此,请阅读响应的正文。

本文链接:https://www.f2er.com/3153277.html

大家都在问