<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ibrahim Sow</title>
    <description>The latest articles on Forem by Ibrahim Sow (@ibrahimsow).</description>
    <link>https://forem.com/ibrahimsow</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3339264%2F4f375318-700a-4c4e-ba17-c2ddfd11a073.jpg</url>
      <title>Forem: Ibrahim Sow</title>
      <link>https://forem.com/ibrahimsow</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ibrahimsow"/>
    <language>en</language>
    <item>
      <title>Why IHttpClientFactory Will Save Your .NET App (and Your TCP Ports!)</title>
      <dc:creator>Ibrahim Sow</dc:creator>
      <pubDate>Sun, 20 Jul 2025 13:43:24 +0000</pubDate>
      <link>https://forem.com/ibrahimsow/why-ihttpclientfactory-will-save-your-net-app-and-your-tcp-ports-50p7</link>
      <guid>https://forem.com/ibrahimsow/why-ihttpclientfactory-will-save-your-net-app-and-your-tcp-ports-50p7</guid>
      <description>&lt;p&gt;As .NET developers, we’ve all done this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var client = new HttpClient();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s easy, it works… until your app starts failing unexpectedly due to port exhaustion.&lt;br&gt;
In this article, I’ll explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why creating multiple &lt;code&gt;HttpClient&lt;/code&gt;instances is dangerous&lt;/li&gt;
&lt;li&gt;How &lt;code&gt;IHttpClientFactory&lt;/code&gt;solves the problem&lt;/li&gt;
&lt;li&gt;How to structure your code using Clean Architecture&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  &lt;strong&gt;❌ The Problem with &lt;code&gt;new HttpClient()&lt;/code&gt;&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;Bad practice: creating HttpClient for every request&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public async Task&amp;lt;string&amp;gt; GetWeatherAsync()
{
    using var client = new HttpClient();
    var response = await client.GetAsync("https://api.weather.com/forecast");
    return await response.Content.ReadAsStringAsync();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Why is this bad?&lt;/strong&gt;&lt;br&gt;
Each &lt;code&gt;new HttpClient()&lt;/code&gt; opens a fresh TCP connection. When used frequently (e.g., in loops or heavy API calls), this leads to port exhaustion, causing your HTTP requests to fail.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  The Right Approach:&lt;code&gt;IHttpClientFactory&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;.NET introduced &lt;code&gt;IHttpClientFactory&lt;/code&gt;to manage reusable and efficient &lt;code&gt;HttpClient&lt;/code&gt;instances.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Define a Service Interface (Clean Architecture)
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public interface IWeatherService
{
    Task&amp;lt;string&amp;gt; GetForecastAsync();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 2: Implement the Service
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class WeatherService : IWeatherService
{
    private readonly HttpClient _httpClient;

    public WeatherService(HttpClient httpClient)
    {
        _httpClient = httpClient;
    }

    public async Task&amp;lt;string&amp;gt; GetForecastAsync()
    {
        var response = await _httpClient.GetAsync("/forecast");
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Step 3: Configure HttpClient in Program.cs
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;builder.Services.AddHttpClient&amp;lt;IWeatherService, WeatherService&amp;gt;(client =&amp;gt;
{
    client.BaseAddress = new Uri("https://api.weather.com");
    client.DefaultRequestHeaders.Add("Accept", "application/json");
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;With &lt;code&gt;IHttpClientFactory&lt;/code&gt;, .NET reuses connections under the hood, improves performance, and avoids port exhaustion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What About You?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Have you ever experienced port exhaustion in production?&lt;/li&gt;
&lt;li&gt;Do you prefer Named Clients or Typed Clients with &lt;code&gt;IHttpClientFactory&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Let me know in the comments – I’d love to hear your experience!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;About Me&lt;/strong&gt;&lt;br&gt;
I’m a C#/.NET &amp;amp; Angular fullstack developer, passionate about building robust SaaS applications and clean, testable architectures&lt;br&gt;
Contact me on &lt;a href="https://www.linkedin.com/in/sow-ibrahima/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
