intentionality - complete
Why is this an issue?
How can I fix it?
More Info
If a code path that creates and disposes of HttpClient objects is frequently used, then the following issues can occur:
- Under heavy load, there’s the risk of running out of available
sockets, leading to SocketException errors. This
is because each HttpClient instance uses a separate network connection, and there’s a limit to the number of connections that can be opened
simultaneously. Note that even after you dispose of an HttpClient its sockets are not immediately freed up.
- Each HttpClient has its own set of resources (like headers, base address, timeout, etc.) that must be managed. Creating a new HttpClient for
every request means these resources are not being reused, leading to resource waste.
- You introduce a significant performance overhead when creating a new HttpClient for every HTTP request.