{
  "id": 4817611,
  "title": "HttpClientFactory: Why new HttpClient() Is Killing Your App",
  "url": "https://urgent.news/2026/09/01/httpclientfactory-why-new-httpclient-is-killing-your-app",
  "topic": "world",
  "section": "World",
  "published": "2026-09-01T08:03:12.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/homolibere/httpclientfactory-why-new-httpclient-is-killing-your-app-2b8o"
  },
  "original_language": "en",
  "account": "C# developers often create an HttpClient instance with a using statement, but this leads to Socket Exhaustion. The problem occurs because disposing of HttpClient doesn't close sockets immediately; they enter a TIME_WAIT state for several minutes. A single Windows machine has only ~16,000 ephemeral ports, so under load, all sockets can be exhausted in just a few minutes. .NET Core 2.1 introduced IHttpClientFactory to solve this issue by pooling and reusing HttpMessageHandler instances, allowing proper DNS rotation. To use it, addHttpClient() in Program.cs or Startup.cs, then create services with IHttpClientFactory. For pre-configured clients, use named clients by adding client = {BaseAddress, DefaultRequestHeaders, Timeout}. For typed clients, inject a fully configured HttpClient directly into your service. Additionally, you can customize handler lifetime, but be cautious with long lifetimes as IP changes can cause issues. The best approach is to stop creating HttpClient instances manually and use IHttpClientFactory to manage connection pooling, avoiding socket exhaustion errors.",
  "summary": "Hey there, fellow C# developers! 👋 Let me tell you a horror story. You're building a nice web service, everything works great in development, and then... production hits. Suddenly your app is throwing SocketException errors left and right. What happened? You probably did this: // DON'T DO THIS! using ( var client = new HttpClient ()) { var response = await client . GetAsync (…",
  "key_points": [
    "Manual HttpClient creation causes Socket Exhaustion",
    "IHttpClientFactory pools and reuses HttpMessageHandler instances",
    "Use IHttpClientFactory to manage connection pooling"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}