-4

What happens when we create client object using

var client=new HttpClient();

And when we use IHttpClientFactory builder.Services.AddHttpClient();

and using it through Dependency Injection

        public readonly IHttpClientFactory _clientFactory;
        
        public RequestController(ClientPolicy clientPolicy,IHttpClientFactory clientFactory)
        {
            _clientFactory = clientFactory;
        }
 
       var client= _clientFactory.CreateClient();

Asad
  • 617
  • 2
  • 8
  • 23
  • 2
    A quick googling reveals you can find your answers [here](https://learn.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests). – JuanR May 05 '22 at 14:35
  • @Mohi have answered my own Question can you kindly verify it – Asad May 05 '22 at 14:51

1 Answers1

0

The Answer learnt from the above comment and articles is that when an HttpClient is Disposed its socket not disposed so soon that's why it create Socket Exhaustion problem and if we create singleton object of HttpClient then there will be DNS Error that's why in . Net core we implements IHttpClientFactory so it can overcome Socket Exhaustion problem and DNS Problem and its easy to maintain IHttpClientFactory at a single class in case of code modifications it will be implemented

Asad
  • 617
  • 2
  • 8
  • 23