HttpClient Socket Exhaustion Fix

Published: May 15, 2026 · By Kumar Kunal

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(); }

Found this helpful?

Master C# with our complete course. Real apps, real skills, job-ready in 2 hours.

Share this fix: Twitter LinkedIn

Comments on HttpClient Socket Exhaustion Fix (0)

No comments yet. Be the first to share your thoughts!