-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What's the recommended approach for reusing/disposing new HttpClient handlers? #16255
Comments
Once you attach the var handler = new HttpClientHandler;
var client = new HttpClient(handler); it is not necessary to But in general, you should share and reuse the |
Closing, as I believe the question has been answered. Please re-open if it hasn't. Thanks. |
@davidsh @stephentoub what about sharing the same |
That is not recommended at all. The handler is owned by the HttpClient once you create the HttpClient. It is an undefined behavior to share the same handler object between separate client objects. |
@davidsh thank you for clarifying. What would you suggest then for such a scenario (shared |
Why not? If the HttpClient is passed the handler instance along with a bool indicating the HttpClient doesn't own it, what breaks? |
After reviewing this some more, you are correct in that using this form of the HttpClient ctor: public HttpClient (System.Net.Http.HttpMessageHandler handler, bool disposeHandler); will mean that the HttpClient object doesn't "own" the handler. So, in theory, you can share the same handler between HttpClient objects. |
@davidsh : From you comment above, it seems that this scenario of using same HttpClientHandler with different HttpClient was kind of not tested/expected by .Net team back then. |
It's been recommended to reuse the full framework's
HttpClientHandler
across multiple requests instead of disposing it every time (because disposing it means the performance hit of closing and re-opening the underlying connection). Does this advice stands true for the brand new world ofCurlHandler
andWinHttpHandler
?The text was updated successfully, but these errors were encountered: