1 year ago

#377880

test-img

el peregrino

HttpClient timeout when requesting accessible webpage

We have implemented a simple health check to verify a website is available. We are facing timeouts in production.

There is a named HTTP client registered. The client sends a simple GET request to a public page of the website. I can reach the page via browser in several seconds. There is the accept header to accept any content type. The request completion is set to headers read. The timeout is set to 30 seconds. The execution takes 30 seconds. The website is behind a load balancer (CloudFront).

The registration:

services.AddHttpClient("ClientName", client =>
{
    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
    client.BaseAddress = ServiceUri;
}).AddHttpMessageHandler<HttpLoggingHandler>();

The handler:

// copy response to memory stream
using MemoryStream stream = recyclableMemoryStreamManager.GetStream();
await response.Content.CopyToAsync(stream);

The response content is written back after the copy. We have multiple clients following the same pattern without an issue.

The call:

using HttpClient httpClient = httpClientFactory.CreateClient(context.Registration.Name);

using HttpResponseMessage response = await httpClient.GetAsync(
                        options.HealthUri,
                        HttpCompletionOption.ResponseHeadersRead,
                        source.Token); // the token expires in 30 secs

Is there any request header missing?

Can be the HttpLoggingHandler issue? It tries to read the request/response content and write it to the logs. Can be HttpCompletionOption.ResponseHeadersRead header causing an issue in this case? Can be the reason in the IHttpClientFactory's internal cache of the message handlers?

The default System.Net.Http.IHttpClientFactory implementation may cache the underlying System.Net.Http.HttpMessageHandler instances to improve performance.

Any ideas are appreciated.

c#

httpclient

asp.net-core-3.1

0 Answers

Your Answer

Accepted video resources