An HttpClient is created for a single use, which can exhaust sockets
What it finds
Each HttpClient holds its own connections, and disposing it leaves its sockets waiting for minutes before the system reclaims them. Creating one per request, as in using var client = new HttpClient(); inside a method, runs out of sockets under load. Share one instance, or create clients through IHttpClientFactory.
From the rule's source, new_http_client.rs.
using System.Net.Http;using System.Threading.Tasks; class Api{ private static readonly HttpClient Shared = new HttpClient(); async Task<string> Get(string url) { using var client = new HttpClient();tx0 reports: a new HttpClient is created for a single use return await client.GetStringAsync(url) + await Shared.GetStringAsync(url); }} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/csharp:new-http-client" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/csharp:new-http-client] reason="..."tx0 explain tx0/csharp:new-http-client shows the same in the terminal. Found a false positive? Report it.