.NET SocketException: Only One Usage of Each Socket Address (HttpClient Exhaustion)
Creating `new HttpClient()` inside `using` blocks leaves sockets in TIME_WAIT.
Runs locally in browser
Free · No login
Independently Verified 2026-11-25
v4.1.0
Identifier: .NET SocketException
Platform: DotNet
Common Root Causes:
- Creating `new HttpClient()` inside `using` blocks leaves sockets in TIME_WAIT.
Interactive Diagnostic Checklist
Verified Solutions:
- Inject `IHttpClientFactory` or reuse a singleton `HttpClient` with `PooledConnectionLifetime`.
Safe Diagnostic Command:
ss -s | grep TIME-WAIT
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.
Technical Explanation
Creating `new HttpClient()` inside `using` blocks leaves sockets in TIME_WAIT. Recommended diagnostic: `ss -s | grep TIME-WAIT`. Remediation: Inject `IHttpClientFactory` or reuse a singleton `HttpClient` with `PooledConnectionLifetime`.
Worked Examples
- Run `ss -s | grep TIME-WAIT` to verify root cause before applying the remediation.
Limitations & Assumptions
- Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.
Source References & Verification
Resource ID: err-dotnet-socket-exhaustion-httpclient · Independently verified: 2026-11-25