تخطَّ إلى المحتوى

Http client lifetime (ARCH1103)

هذا المحتوى غير متوفر بلغتك بعد.

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.

public async Task<string> GetAsync(string url)
{
using var client = new HttpClient();
return await client.GetStringAsync(url);
}
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.