使用 HttpClient 访问服务时出现 SocketException (0x274c)
SocketException (0x274c) while accessing service with 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)。
在我的本地,一切都运行干净。我成功得到回应。
但是当我将我的代码部署到服务器时它工作了几次干净然后 return 吼叫 服务器错误 .
A connection attempt failed because the connected party did not
properly respond after a period of time, or established connection
failed because connected host has failed to respond xxx.xx.xx.xxx:443
StackTrace
[SocketException (0x274c): A connection attempt failed because the
connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to
respond xxx.xx.xx.xxx:443]
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
+8439919 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress&
address, ConnectSocketState state, IAsyncResult asyncResult,
Exception& exception) +315
[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult
asyncResult, TransportContext& context) +322
System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult
ar) +137
[HttpRequestException: An error occurred while sending the request.]
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +60 AAA.XXXX.Mvc.Controllers.d__14.MoveNext() in
C:\Projects\AAA.XXXX\src\AAA.XXXX.Mvc\Controllers\SearchController.cs:276
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +60
AAA.XXXX.Mvc.Controllers.d__11.MoveNext() in
C:\Projects\AAA.XXXX\src\AAA.XXXX.Mvc\Controllers\SearchController.cs:192
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +60 AAA.XXXX.Mvc.Controllers.d__6.MoveNext() in
C:\Projects\AAA.XXXX\src\AAA.XXXX.Mvc\Controllers\SearchController.cs:87
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task
task) +60
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult
asyncResult) +92
System.Web.Mvc.Async.<>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.<>c__DisplayClass46.b__3f()
+385 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult
asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c()
+38 System.Web.Mvc.Async.<>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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult
asyncResult) +65<br>
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) 的主题,但没有找到对我有用的解决方案。可能在我的服务器上有一个与网络相关的错误,但我没有解决它。
我解决了这个问题。网络人员向服务器添加了一个负载平衡器,如果应用程序使用未列入服务白名单的不同池 IP,那么我们会遇到此异常。感谢您的评论、时间和帮助。
我在这段代码中有一个 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)。
在我的本地,一切都运行干净。我成功得到回应。
但是当我将我的代码部署到服务器时它工作了几次干净然后 return 吼叫 服务器错误 .
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xx.xx.xxx:443
StackTrace
[SocketException (0x274c): A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond xxx.xx.xx.xxx:443]
System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) +8439919 System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception) +315[WebException: Unable to connect to the remote server]
System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) +322
System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) +137[HttpRequestException: An error occurred while sending the request.]
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60 AAA.XXXX.Mvc.Controllers.d__14.MoveNext() in C:\Projects\AAA.XXXX\src\AAA.XXXX.Mvc\Controllers\SearchController.cs:276 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
AAA.XXXX.Mvc.Controllers.d__11.MoveNext() in C:\Projects\AAA.XXXX\src\AAA.XXXX.Mvc\Controllers\SearchController.cs:192 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Tasktask) +60 AAA.XXXX.Mvc.Controllers.d__6.MoveNext() in C:\Projects\AAA.XXXX\src\AAA.XXXX.Mvc\Controllers\SearchController.cs:87 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() +31
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +60
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +92
System.Web.Mvc.Async.<>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.<>c__DisplayClass46.b__3f() +385 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +38 System.Web.Mvc.Async.<>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.WrappedAsyncVoid1.CallEndDelegate(IAsyncResult asyncResult) +65<br> 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) 的主题,但没有找到对我有用的解决方案。可能在我的服务器上有一个与网络相关的错误,但我没有解决它。
我解决了这个问题。网络人员向服务器添加了一个负载平衡器,如果应用程序使用未列入服务白名单的不同池 IP,那么我们会遇到此异常。感谢您的评论、时间和帮助。