'What is the Difference between IHttpClientFactory and HttpClient [duplicate]

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



Solution 1:[1]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Asad