There are many resources asking to re-use HttpClient
instances to avoid socket exhaustion. In that approach, one should not dispose the HttpClient
instances.
But similar to the ask in the first comment in this answer asks:
would it be correct to say that “you do not need to dispose of
HttpClient
IF YOU HOLD ON TO THE INSTANCE TO REUSE IT LATER”? For instance, if a method is called repeatedly and creates a newHttpClient
instance (even though it’s not the recommended pattern in most cases), would it still be correct to say this method should not dispose the instance (that will not be reused)? It could lead to thousands of undisposed instances.
I want to know if I should dispose using the using
keyword, in an app that does not follow the pattern, and creates instances for every request.
Please do not recommend to change the design to re-use, because that will be done in due time, but suggest what is the proper way to use when the creation happens for each request.
For dot net core.