I have a .Net Core 3.1 app. In the app, I use the IHttpClientFactory to create an HttpClient. When I make a call using SendAsync, the first request takes over 2 seconds whereas subsequent requests take less than 100 ms. This is not acceptable performance for a production application.
I have also noticed that it happens if I don't make any requests for a while. I came across the PooledConnectionIdleTimeout property, which defaults to 2 minutes, and I can extend that time, but that would only work for pooled connections that already exist, not when needing to create a new one.
I configure the HttpClient in my Startup.cs as such:
services.AddHttpClient("Default")
.ConfigurePrimaryHttpMessageHandler(() =>
{
return new SocketsHttpHandler
{
UseCookies = false,
AllowAutoRedirect = false,
MaxConnectionsPerServer = int.MaxValue,
UseProxy = false
};
});
I have a Singleton wrapper class which contains a method which calls SendAsync:
public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, HttpCompletionOption completionOption, CancellationToken cancellationToken = default)
{
var client = m_HttpClientFactory.CreateClient("Default");
return await client.SendAsync(request, completionOption, cancellationToken).ConfigureAwait(false);
}
Is there some kind of configuration that I'm missing? Or some kind of warm up that I can do so that the first request doesn't take a long time?
UPDATE: It's been a few months and I don't remember all of the details, but considering a recent comment, I decided to post my findings and my workaround.
I found that this issue did not occur when using an IP address for the hostname rather than the server name. In my case, I'd almost always be calling localhost, so I do a comparison the hostname I'm supposed to reach out to and if it matches the local system's host name, I change the host to 127.0.0.1.
IIRC, the .Net Core logic will try to fetch all IPs for the server name and then go through each one, one by one, and try to connect successfully. Depending on how many it has to cycle through will increase the time by 1 second per failed attempt. When I had IPv6 enabled on my system, that doubled the amount of IPs it would potentially try.
Hope that helps and sorry if it sounds vague. It's been a while :)