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

Avoid creating a new HttpClient per HttpWebRequest in some cases #20013

Closed
stephentoub opened this issue Jan 25, 2017 · 2 comments · Fixed by dotnet/corefx#41462
Closed

Avoid creating a new HttpClient per HttpWebRequest in some cases #20013

stephentoub opened this issue Jan 25, 2017 · 2 comments · Fixed by dotnet/corefx#41462
Assignees
Labels
area-System.Net.Http enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Milestone

Comments

@stephentoub
Copy link
Member

In .NET Core, HttpWebRequest is layered on top of HttpClient and HttpClientHandler. Due to the mismatch in APIs, the implementation creates a new HttpClient per HttpWebRequest, which is very inefficient. But those costs can be avoided in some circumstances. In particular, we can have a shared HttpClient configured with the same defaults that HttpWebRequest has; then as long as none of the settings on the HttpWebRequest have been changed to veer from the defaults, the shared HttpClient instance can be used rather than creating a new one.

@khellang
Copy link
Member

khellang commented Jan 25, 2017

Just curious, would/could this get impacted by https://github.com/dotnet/corefx/issues/11224?

@alnikola alnikola self-assigned this Sep 23, 2019
alnikola referenced this issue in dotnet/corefx Oct 8, 2019
HttpWebRequest caches and tries to reuse a single static HttpClient instance when it's safe to share the same instance among concurrent requests with the given parameters.
Fixes #15460
@alnikola
Copy link
Contributor

Currently, the caching was implemented with the simplest possible strategy which caches and reuses the static single instance of HttpClient when the given HttpWebRequest parameters match to the ones of the cached instance. Moreover, caching is only allowed if all security and proxy related parameters are set to null or default values. Further, this can be improved by creating a pool of HttpClients matching to different combinations of HttpWebRequest parameters, and potentially permitting to cache some of security or proxy related settings as well.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 5.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Net.Http enhancement Product code improvement that does NOT require public API changes/additions help wanted [up-for-grabs] Good issue for external contributors tenet-performance Performance related issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants