System.Net.Http.HttpRequestException: No connection could be made because the target machine actively refused it. SocketException: No connection could be made because the target machine actively refused it. (127.0.0.1:443)
ID: dotnet/httpclient-socket-connection-pool-exhausted
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| .NET Core 3.1 | active | — | — | — |
| .NET 5.0 | active | — | — | — |
| .NET 6.0 | active | — | — | — |
| .NET 7.0 | active | — | — | — |
| .NET 8.0 | active | — | — | — |
Root Cause
HttpClient instances are not being disposed properly, leading to socket exhaustion and port starvation as each instance holds onto TCP connections for longer than necessary, especially under high concurrency.
generic中文
HttpClient 实例未正确释放,导致套接字耗尽和端口枯竭,每个实例在并发高的情况下长时间占用 TCP 连接。
Official Documentation
https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelinesWorkarounds
-
95% success Use IHttpClientFactory to manage HttpClient instances. Register it in DI: services.AddHttpClient(); Then inject IHttpClientFactory and create clients with factory.CreateClient(). This reuses connections and avoids socket exhaustion.
Use IHttpClientFactory to manage HttpClient instances. Register it in DI: services.AddHttpClient(); Then inject IHttpClientFactory and create clients with factory.CreateClient(). This reuses connections and avoids socket exhaustion.
-
90% success If not using DI, use a static or singleton HttpClient instance and ensure it is not disposed. Example: private static readonly HttpClient _httpClient = new HttpClient();
If not using DI, use a static or singleton HttpClient instance and ensure it is not disposed. Example: private static readonly HttpClient _httpClient = new HttpClient();
-
70% success Increase the default connection limit per remote endpoint using ServicePointManager: ServicePointManager.DefaultConnectionLimit = 100; Place this in application startup.
Increase the default connection limit per remote endpoint using ServicePointManager: ServicePointManager.DefaultConnectionLimit = 100; Place this in application startup.
中文步骤
Use IHttpClientFactory to manage HttpClient instances. Register it in DI: services.AddHttpClient(); Then inject IHttpClientFactory and create clients with factory.CreateClient(). This reuses connections and avoids socket exhaustion.
If not using DI, use a static or singleton HttpClient instance and ensure it is not disposed. Example: private static readonly HttpClient _httpClient = new HttpClient();
Increase the default connection limit per remote endpoint using ServicePointManager: ServicePointManager.DefaultConnectionLimit = 100; Place this in application startup.
Dead Ends
Common approaches that don't work:
-
Creating a new HttpClient instance for each request inside a using block.
90% fail
Disposing HttpClient too frequently causes socket exhaustion because each instance opens a new TCP connection but does not reuse sockets efficiently.
-
Increasing the connection limit in the app.config or machine.config.
70% fail
While this can help, it does not address the root cause of not reusing connections; it only delays exhaustion.
-
Setting ServicePointManager.DefaultConnectionLimit to a high value.
80% fail
This is a global setting that affects all HttpClient instances and may not be sufficient if instances are created and disposed frequently.