ReSharper 2026.1 Help

Code inspection: Short-lived 'HttpClient'

This inspection identifies cases where HttpClient is instantiated within a short-lived scope, such as inside a method or a using block.

Although HttpClient implements IDisposable, it is designed to be instantiated once and reused throughout the life of an application. Creating a new instance for every request can lead to socket exhaustion. This happens because even after the HttpClient is disposed, the underlying sockets are not immediately released by the operating system (they stay in the TIME_WAIT state).

public class Connection { static async Task<string> GetDataAsync0(string url) { var client = new HttpClient(); var response = await client.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } }
public class Connection { private static readonly HttpClient Client = new(); static async Task<string> GetDataAsync0(string url) { var response = await Client.GetAsync(url); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } }

The quick-fix for this inspection promotes the HttpClient instance to a private static readonly field, ensuring it is reused across multiple calls.

11 March 2026