{
  "id": "dotnet/httpclient-socket-connection-pool-exhausted",
  "signature": "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)",
  "signature_zh": "System.Net.Http.HttpRequestException: 无法建立连接，因为目标计算机主动拒绝连接。SocketException: 无法建立连接，因为目标计算机主动拒绝连接。(127.0.0.1:443)",
  "regex": "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\\. \\(.+\\)",
  "domain": "dotnet",
  "category": "network_error",
  "subcategory": null,
  "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.",
  "root_cause_type": "generic",
  "root_cause_zh": "HttpClient 实例未正确释放，导致套接字耗尽和端口枯竭，每个实例在并发高的情况下长时间占用 TCP 连接。",
  "versions": [
    {
      "version": ".NET Core 3.1",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 5.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 6.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 7.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": ".NET 8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Creating a new HttpClient instance for each request inside a using block.",
      "why_fails": "Disposing HttpClient too frequently causes socket exhaustion because each instance opens a new TCP connection but does not reuse sockets efficiently.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Increasing the connection limit in the app.config or machine.config.",
      "why_fails": "While this can help, it does not address the root cause of not reusing connections; it only delays exhaustion.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "Setting ServicePointManager.DefaultConnectionLimit to a high value.",
      "why_fails": "This is a global setting that affects all HttpClient instances and may not be sufficient if instances are created and disposed frequently.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "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.",
      "success_rate": 0.95,
      "how": "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.",
      "condition": "",
      "sources": []
    },
    {
      "action": "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();",
      "success_rate": 0.9,
      "how": "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();",
      "condition": "",
      "sources": []
    },
    {
      "action": "Increase the default connection limit per remote endpoint using ServicePointManager: ServicePointManager.DefaultConnectionLimit = 100; Place this in application startup.",
      "success_rate": 0.7,
      "how": "Increase the default connection limit per remote endpoint using ServicePointManager: ServicePointManager.DefaultConnectionLimit = 100; Place this in application startup.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "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."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.86,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-06-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}