Http client lifetime (ARCH1103)
Esta página aún no está disponible en tu idioma.
Key dotnet.resource.http-client-lifetime · Severity error · Category resource
Creating HttpClient instances directly with new leads to socket exhaustion. Use clients managed through IHttpClientFactory.
Incorrect
Section titled “Incorrect”public async Task<string> GetAsync(string url){ using var client = new HttpClient(); return await client.GetStringAsync(url);}Correct
Section titled “Correct”public PriceClient(IHttpClientFactory factory) => _factory = factory;
public async Task<string> GetAsync(string url){ var client = _factory.CreateClient(); return await client.GetStringAsync(url);}Replace new HttpClient() calls with IHttpClientFactory, letting the factory manage client lifetime and the connection pool.