Skip to content

Replace new HttpClient() per-call with a shared/reusable instance #36

Description

@DaraOladapo

Description

Every method in the library creates a disposable HttpClient inside a using block:

using (HttpClient httpClient = new HttpClient())
{
    ...
}

This is an established .NET anti-pattern. Each construction opens a new socket connection; disposing the client does not immediately release the underlying socket — it enters TIME_WAIT. Under any meaningful load this causes socket exhaustion and SocketException.

References

Recommended Fix

Option A — Static singleton (simple, no DI required for a library):

private static readonly HttpClient _httpClient = new HttpClient();

Option B — Accept IHttpClientFactory or HttpClient as a constructor/method parameter to allow callers to manage the lifetime (better for testability and named clients).

Scope

All five partial-class files: Get.cs, Post.cs, Put.cs, Patch.cs, Delete.cs.

Acceptance Criteria

  • No new HttpClient() inside method bodies
  • Socket exhaustion cannot occur from normal library usage
  • Existing public API signatures remain unchanged (non-breaking)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    Status
    In Progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions