1

I have a very strange scenario I keep experiencing on one of my systems.

I have an asp.net web API hosted in IIS. Web requests come into it from mobile devices, and then it posts HTTP requests to another server that executes the specific job and then returns the result. (see flow below)

Phone => API => HTTP => ( Service Executor ) => HTTP => API => PHONE

This works perfectly 95% of the time. However it sometimes gets into this state where NONE of the http requests complete.

I have observed the following:

  • After the issues happens once, it will continue to fail indefinitely until i restart the application pool.
  • Setting the IIS application pool recycle time to 1 hour has significantly reduced the occurrence. However not totally removed it.

The following method is called from my Asp.net Controller:

Here is my code:

public static async Task<string> MakeRequest(string deviceId, BaseCommand baseCommand)
{
    using (var client = new HttpClient())
    {
        // Setup Client                
        client.BaseAddress = await GetBaseAddressForDevice(deviceId);
        client.DefaultRequestHeaders.Accept.Clear();
        //Only wait 90 seconds for responses
        client.Timeout = new TimeSpan(0, 0, 0, 90);

        var requestId = Guid.NewGuid();

        Debug.WriteLine(">Posting  RequestId: " + requestId + " commandId:" + ((int)(baseCommand.CommandId)).ToString(CultureInfo.InvariantCulture) + " data: " + JsonConvert.SerializeObject(baseCommand));
        //

        // Make request
        HttpResponseMessage response = null;
        string responseContent = null;
        try
        {
            response = await client.PostAsJsonAsync(((int)baseCommand.CommandId).ToString(CultureInfo.InvariantCulture), baseCommand);
            // read response content
            responseContent = await response.Content.ReadAsStringAsync();
        }
        catch (Exception ex)
        {
            //TODO: Log excepton
            throw ex;
        }

        Debug.WriteLine(">Response  RequestId: " + requestId + " data: " + JsonConvert.SerializeObject(baseCommand));
        //

        if (response.IsSuccessStatusCode)
        {
            return responseContent;
        }
        HandleErrorResponse(response, responseContent);
        //Wont hit here as an exception will be thrown
        return null;
    }
}

The above example is designed to create and dispose of an HttpClient instance each time. I believe that HttpClient is thread safe and I have tested before with a single instance of HttpClient, and I had the same issues.

Further Behavior that I have observed.

I added logging on the server that Receives the http posts. When this phenomenon happens, it does receive the HTTP Post, however it then immediately fails. Almost like it establishes the connection, and then fails.

I have some exceptions which Application Insights has collected:

 - System.Net.Sockets.SocketException
An error occurred while sending the request.The underlying connection was closed: An unexpected error occurred on a receive.Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.An existing connection was forcibly closed by the remote host

I would really love some input on the issue, maybe some further suggestions to try and debug / location this issue further.

Zapnologica
  • 22,170
  • 44
  • 158
  • 253
  • The error message you included (`System.Net.Sockets.SocketException`), is that logged by the Web Api or the server that it calls? Which line in the server code throws the exception? – user1429080 Feb 06 '17 at 07:31
  • 1
    Have you seen, read, and tested what the author says [here](https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/). Seems it could be worth trying. – user1429080 Feb 09 '17 at 15:16
  • Have you got the solution? Please share me your. I have the same problem. – Rango Jul 06 '23 at 10:26

0 Answers0