runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Avoid creating a new HttpClient per HttpWebRequest in some cases

Open stephentoub opened this issue 9 years ago • 2 comments

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.

stephentoub avatar Jan 25 '17 12:01 stephentoub

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

khellang avatar Jan 25 '17 12:01 khellang

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.

alnikola avatar Oct 21 '19 10:10 alnikola