0
votes

I have a httpClient code piece in this piece of code I requested (PostAsync) 3 times to the service like;

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;
    }
}

As you can see inside of the steps I send a request(postAsync) to the same service.

In my local everything is run clean. I got response successfully.

But when I deploy my codes to the server It works few times clean then return bellowing server error.

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
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

I checked many topics about socket error (0x274c) but I didn't find any useful solution for me. Probably on my server has a network related error but I didn't solve it.

1
You shouldn't be creating a new instance of HttpClient every time. Microsoft says to create a static instance, and this explains why. - Llama
I don't create a new instance of httpClient. I passed which I create in using. @john I am passing the same client. I edited the code - arslanaybars
But thank you. I am going to try to create static httpClient now. I tried with static httpClient. the result is same first 2, 3 trying its working then it's not. same error - arslanaybars
Yes, but each time you call SendRequestToTheService you're creating a new HttpClient, which creates a new thread pool, which isn't disposed of for some time after your code completes. It might not be your issue, but it might help. - Llama
with telnet, can you open that domain or ip address on port 443? - MichaelEvanchik

1 Answers

1
votes

I solved the problem. The network guy added a load balancer to the server and if the application uses different pool IP's which is not whitelisted in service so we got this exception. Thanks for comments, time and helps.