Slow outbound API calls often come from how HttpClient and its underlying handler pool connections, not from endpoint logic. The article covers socket exhaustion, stale DNS, handler lifetime, MaxConnectionsPerServer and the System.Net.Http metrics that reveal requests waiting for a free connection.
Key points
- Building a new HttpClient per request creates a fresh handler and connection pool, so connections are never reused and ports sit in TIME-WAIT.
- PooledConnectionLifetime on SocketsHttpHandler retires a connection after a set span, letting a later request do a fresh DNS lookup.
- IHttpClientFactory.CreateClient returns a new HttpClient each time but pools the underlying handlers; SetHandlerLifetime controls handler rotation.
- MaxConnectionsPerServer caps simultaneous HTTP/1.1 connections; check http.client.request.time_in_queue before raising it.
- The .NET 8 networking metrics http.client.request.time_in_queue and http.client.open_connections show requests blocked waiting for a connection.
- Feed a CancellationToken into typed clients, and never capture one in a singleton or the handler cannot rotate.
Real-world use
- An ASP.NET Core service calling a payment provider, an auth provider and internal microservices, each needing its own timeout.
- An integration with an external API behind a load balancer whose IP address changes while the application runs.
- A high-traffic app sending hundreds of concurrent requests to one external API, where requests queue for a free connection.
This summary was generated by the agent from the original post on C# Corner. The full text and all rights belong to its author and publisher. Go to original