HttpClient.SendAsync takes a lot longer to receive response from the api than expected

I have a simple program that uses System.Net.Http.HttpClient which works like this:

// Get existing or create new httpclient if there isn't one instace of it already  mClient = GetHttpClient(); var startTime = DateTime.Now;  var res = await mClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);  var executeMethodTime = DateTime.Now.Subtract(startTime).TotalMilliseconds; mLog.Info("restPostExecuteMethodTime : {0}", executeMethodTime); 

It makes a simple request to the API and waits for the response. The content of the request is very small just some simple objects nothing too big.

The API side looks like this:

public async Task<IHttpActionResult> DoWork([FromBody]TemplateInput templateInput) {         IHttpActionResult res;         var startTime = DateTime.Now;          // do logic          var auditTime = DateTime.Now.Subtract(startTime).TotalMilliseconds;         mLog.Info($"auditTime {auditTime}");         return res; } 

Here is the logging generated by that code :

2020-07-15 09:12:19.7472|auditTime 204.9607| 2020-07-15 09:12:24.8172|executeMethodTime : 5321.9861| 

What surprised me is that the API took over 5 seconds to return the result when in fact all the logic in the API took only about 200 milliseconds to finish executing. But it took whole entire 5 seconds to return the result back to the requester.

I checked the network and there were no problems.

It made no sense to me. Is there anything in HttpClient that could potentially affect the response speed? To the point that it would cost request 5 seconds?

I first thought it was caused by proxy setting inside httpClient so I tried to set proxy to false when creating httpClient it turned out to make no difference to speed.

If anyone has any idea – please let me know.

Add Comment
0 Answer(s)

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.