We live in a world of microservices and, in the .NET world, these microservices are quite ofteninvoked using APIs, such asHttpClient. The problem withHttpClientis that it is often misused because, even though it implementsIDisposable, it is really not meant to be disposed of after each usage, but rather should be reused. It is thread-safe and you should have a single instance of it per application.
Disposing of it circumvents the original purpose of the class and, because the contained native socket is not immediately disposed of, if you instantiate and dispose of manyHttpClientAPIs in this way, you may end up exhausting your system's resources.
.NET Core 2.1 introduced HttpClient factories for creating and maintaining pools of pre-configured HttpClient APIs. The idea is simple—register a named client with a base URL and possibly some options (such as headers and a timeout) and inject them whenever needed. When it is no longer...