Why Your .NET App Silently Fails: The TCP Port Exhaustion Trap
ASP.NET Core applications can suffer mysterious outbound API failures even when CPU, memory, and database metrics appear normal, with the root cause hidden at the TCP layer. Each new HttpClient instance created per request opens a fresh TCP connection requiring a unique local port, and closing connections does not immediately free those ports due to TCP's TIME_WAIT state. Under high traffic, an application can exhaust the finite pool of available outbound ports, triggering SocketException errors that disappear temporarily after a restart. Microsoft explicitly warns against creating and disposing HttpClient instances per request, recommending instead that developers reuse HttpClient or use IHttpClientFactory to share connection pools. Understanding this TCP lifecycle explains why the pattern is dangerous and helps developers recognize the same failure even across different technologies or frameworks.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in