1

I have an ASP.NET Web API Z that is calling two services, A and B.

The client calling Z will timeout after 500ms. I have dedicated 150ms to getting a response from A before timing out, and 300ms to B before timing out. The timeout for both of these HttpClients is set using the Timeout property.

When the timeout for B is set to 300ms, I do not get a response in 300ms. I get a TaskCancelledException.

However, when I set the timeout for B to 1 second, I get an end-to-end response of ~180-240ms, less than the timeout for B itself.

Here's the relevant code:

var client = new HttpClient()
{
    BaseAddress = baseAddress,
    Timeout = TimeSpan.Parse("00:00:00.300");
};

using (HttpResponseMessage response = await client.PostAsync(_endpoint, content))
{
    // ...
}

Exception:

System.Net.WebException occurred
Message: Exception thrown: 'System.Net.WebException' in System.dll
Additional information: The request was aborted: The request was canceled.

Is there something that I"m doing wrong? Can someone explain this strange behavior?

Zymus
  • 1,673
  • 1
  • 18
  • 38
  • Could it be that timeouts and timers in general is not that precise? – Michael Dec 13 '16 at 12:21
  • Also see if this helps: http://stackoverflow.com/questions/26313172/whats-the-difference-between-httpclient-timeout-and-using-the-webrequesthandler – Michael Dec 13 '16 at 12:22

0 Answers0