title: IHttpClientFactory Singleton .NET Framework
body: <p><strong>Scenario</strong></p>
  <p>I am trying to change my existing <code>HttpClient</code> to <code>IHttpClientFactory</code>. When I verified the existing code, its using using{...} statement which causes issues and it is mentioned <a href="https://aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong/" rel="nofollow noreferrer">here</a>. So I thought of implementing singleton Http client and reached another blog related to this and it is <a href="http://byterot.blogspot.com/2016/07/singleton-httpclient-dns.html" rel="nofollow noreferrer">here</a>.</p>
  <p>From all these, I understood that the best one is <a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1" rel="nofollow noreferrer"><code>IHttpClientFactory</code></a> introduced in .NET Core.</p>
  <p><strong>Implementation Plan</strong></p>
  <p>As this application is in ASP.NET MVC 4 and does not use DI, I have to do something to use without the DI framework. Based on my search, got answers from StackOverflow and planned to implement the same way. Meanwhile, I also got another project, which already removed all the dependencies and is ready to use in earlier projects without doing all things. The repo is <a href="https://github.com/uhaciogullari/HttpClientFactoryLite" rel="nofollow noreferrer"><code>HttpClientFactoryLite</code></a>.</p>
  <p><strong>Question</strong></p>
  <p>Now I can use <code>HttpClientFactoryLite</code> by initializing this class? The description also mentioned it can be used along with the existing DI framework so that <code>ClientFactory</code> can be registered as a singleton. Please find the wordings from the readme</p>
  <pre><code>using HttpClientFactoryLite;
  
  var httpClientFactory = new HttpClientFactory(); //bliss
  </code></pre>
  <blockquote>
  <p>If you are using dependency injection, make sure that IHttpClientFactory is registered as a singleton.</p>
  </blockquote>
  <p>In my scenario, I don't have any DI framework added. So I am going to initialize the factory wherever I needed. Here I am confused that in 2 things</p>
  <ol>
  <li><p>Is it necessary to make a singleton class for <code>HttpClientFactoryLite</code>?</p>
  </li>
  <li><p>How is this <code>HttpClientFactory</code> class disposed? Is there a need to dispose of it as part of the controller or same using statement etc?</p>
  </li>
  <li><p>Based on the answer from <a href="https://stackoverflow.com/questions/51474776/how-do-i-implement-ihttpclientfactory-in-net-framework-apart-from-net-core">this</a>,  <code>Microsoft.Extensions.Http</code> provides the HttpClientFactory only, not the new optimized HttpClient. This is only available in .NET Core 2.1. So any difference in implementing IHttpClientFactory?</p>
  </li>
  </ol>
  <p>Please advise</p>