使用HttpClient

时间:2018-04-16 13:44:21

标签: c# asp.net iis dotnet-httpclient

我在这段代码中有一个 httpClient 代码片段(PostAsync)3次到服务中就像;

private async Task<Data> SendRequestToTheService(string searchTerm)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(baseUrl);

        Response response = new Response();

        await Step1(param1, client);

        await Step2(param1, param2, client);

        response = await Step3(param, client);
        return response;
    }
}

正如您在步骤中看到的,我向同一服务发送请求( postAsync )。

在我当地的一切都很干净。我成功回应了。

但是,当我将代码部署到服务器时,它可以清理几次,然后返回服务器错误

  

连接尝试失败,因为连接方没有   在一段时间后正确回应,或建立连接   失败,因为连接的主机无法响应xxx.xx.xx.xxx:443

栈跟踪

  

[ SocketException(0x274c):连接尝试失败,因为   连接方在一段时间后没有正确回应,或者   建立的连接失败,因为连接的主机失败了   回复xxx.xx.xx.xxx:443]
  System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)   +8439919 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket&amp; socket,IPAddress&amp;   地址,ConnectSocketState状态,IAsyncResult asyncResult,   异常和放大器;例外)+315

     

[ WebException:无法连接到远程服务器]
  System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult的   asyncResult,TransportContext&amp;上下文)+322
  System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult的   ar)+137

     

[ HttpRequestException:发送请求时发生错误。]
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+60 AAA.XXXX.Mvc.Controllers.d__14.MoveNext()in   C:\项目\ AAA.XXXX的\ src \ AAA.XXXX.Mvc \ \控制器SearchController.cs:276   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+60
  AAA.XXXX.Mvc.Controllers.d__11.MoveNext()in   C:\项目\ AAA.XXXX的\ src \ AAA.XXXX.Mvc \ \控制器SearchController.cs:192   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务

     

任务)+60 AAA.XXXX.Mvc.Controllers.d__6.MoveNext()in   C:\项目\ AAA.XXXX的\ src \ AAA.XXXX.Mvc \ \控制器SearchController.cs:87   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()+31
  System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务   任务)+60
  System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult的   asyncResult)+92
  System.Web.Mvc.Async&LT;&GT; c__DisplayClass37.b__36(IAsyncResult的   asyncResult)+22
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult的   asyncResult)+42
  System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d()   +72 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass46.b__3f()   +385 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult)   asyncResult)+42
  System.Web.Mvc.Async&LT;&GT; c__DisplayClass2b.b__1c()   +38 System.Web.Mvc.Async。&lt;&gt; c__DisplayClass21.b__1e(IAsyncResult)   asyncResult)+185
  System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult的   asyncResult)+38
  System.Web.Mvc.Controller.b__1d(IAsyncResult的   asyncResult,ExecuteCoreState innerState)+29
  System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +65
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52 System.Web.Mvc.Async.WrappedAsyncVoid
1.CallEndDelegate(IAsyncResult的   asyncResult)+36 System.Web.Mvc.Controller.EndExecute(IAsyncResult)   asyncResult)+38
  System.Web.Mvc.MvcHandler.b__5(IAsyncResult的   asyncResult,ProcessRequestState innerState)+43
  System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult的   asyncResult)+65
  System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)   +38 System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar)   +212 System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult)   ar)+166

我查了很多关于套接字错误的主题(0x274c),但我没有找到任何有用的解决方案。可能在我的服务器上有网络相关的错误,但我没有解决它。

1 个答案:

答案 0 :(得分:1)

我解决了这个问题。网络人员在服务器上添加了一个负载均衡器,如果应用程序使用了不在服务中列入白名单的不同池IP,那么我们得到了这个例外。感谢您的评论,时间和帮助。