TL;DR
Don't new HttpClient() per request. Use IHttpClientFactory.
The Error
System.Net.Sockets.SocketException: Only one usage of each socket address
Root Cause
HttpClient doesn't close sockets immediately. Creating thousands = port exhaustion.
Fix
// Program.cs
builder.Services.AddHttpClient();
// Inject
public class ApiService(IHttpClientFactory factory)
{
var client = factory.CreateClient();
}
No comments yet. Be the first to share your thoughts!