Skip to content
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

Closed
HellBrick opened this issue Jan 31, 2016 · 9 comments
Closed
Labels
area-System.Net documentation Documentation bug or enhancement, does not impact product or test code
Milestone

Comments

@HellBrick
Copy link

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 of CurlHandler and WinHttpHandler?

@stephentoub
Copy link
Member

Yes.

cc: @davidsh, @CIPop

@davidsh
Copy link
Contributor

davidsh commented Jan 31, 2016

Once you attach the HttpClientHandler to the HttpClient via:

var handler = new HttpClientHandler;
var client = new HttpClient(handler);

it is not necessary to Dispose the handler directly since disposing the client will dispose the handler as well.

But in general, you should share and reuse the HttpClient. this is the only way to share TCP the pool of TCP connections.

@stephentoub
Copy link
Member

Closing, as I believe the question has been answered. Please re-open if it hasn't. Thanks.

@ohadschn
Copy link

ohadschn commented Jul 5, 2017

@davidsh @stephentoub what about sharing the same HttpClientHandler (be it the default or WinHttpHandler) between different HttpClient instances with different DefaultRequestHeaders?

@davidsh
Copy link
Contributor

davidsh commented Jul 6, 2017

what about sharing the same HttpClientHandler (be it the default or WinHttpHandler) between different HttpClient instances.

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.

@ohadschn
Copy link

ohadschn commented Jul 6, 2017

@davidsh thank you for clarifying. What would you suggest then for such a scenario (shared HttpClient but different request headers)? I can only think of wrapping HttpClient with my wrapper that translates all calls to a SendAsync call with the proper headers. However that's a lot of overloads to implement (GetAsync, DeleteAsync, PostJsonAsync etc)...

@stephentoub
Copy link
Member

That is not recommended at all.

Why not? If the HttpClient is passed the handler instance along with a bool indicating the HttpClient doesn't own it, what breaks?

@davidsh
Copy link
Contributor

davidsh commented Jul 6, 2017

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.

@vindhyavasini1
Copy link

@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.
Did you guys test that scenario after that to say that it is safe to use these two classes in this manner to avoid TCP connection exhaustion in case of heavy load?

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 1.0.0-rtm milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net documentation Documentation bug or enhancement, does not impact product or test code
Projects
None yet
Development

No branches or pull requests

6 participants