If you are creating HttpClient everytime you make an outbound http call from your application then with all due respect you are doing it wrong. There are multiple resources available across the internet that explain the fact that HttpClient instances should be reused throughout the lifetime of the application as it provides better throughput.
If you create an instance of httpclient for each outbound http call, as the number of parallel outbound increases, you will start to see issues like port exhaustion or connection error etc.
Sources: Link # 1 Link # 2
Best Practices?
If you create an instance of httpclient for each outbound http call, as the number of parallel outbound increases, you will start to see issues like port exhaustion or connection error etc.
Sources: Link # 1 Link # 2
Best Practices?
1. Even though HttpClient should be reused, it is to be kept in mind that the responsibility is on you to ensure that your code does not cause trouble. For example -
a. You should maintain different HttpClients for each base URL. If you try to manipulate base urls for parallel calls, you are asking for trouble.
b. Any Http headers that you intend to send as part of Http request, should be included and sent as part of HttpRequestMessage. We should avoid setting headers in HttpClient.DefaultHeaders and manipulating them for each call. Headers in HttpClient are .NET dictionary (not ConcurrentDictionary) and therefore it is not thread-safe :).
2. It is better to use any of the DI containers to create and maintain Singleton instance of HttpClient or a custom Factory.
3. Any other ServiceApiClients that use Http as underlying protocol should either ensure that HttpClient is resued or you need to ensure that you its implementation is such that you can reuse a single instance of that across calls. In fact, there are many out of the box Azure SDK Clients recommend that instances should be resued e.g. DocumentDbClient, ServiceBusClient, Azure Storage Client etc.