-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Is there will be a truely IDisposable
HttpClient
#27601
Comments
What problem are you hoping to see solved? |
@stephentoub the current using(var client = new HttpClient())
{
//will facing probem in height concurrent : number of TCP connections drain .
// is this problem solved by SocketsHttpHandler ?
} and that's one reason of why we should use before using |
Can we solve those annoying concurrent problem once for all ? Or there's a guidance for solve those problem? |
HttpClient is meant to be stored and reused over and over; it also offers Dispose in order to give you explicit control over when its resources/connections are torn down, whereas you don't have such explicit control with WebRequest, where the underlying pool isn't exposed. What are you trying to do in your library where this doesn't work for you? |
@John0King the guidance is to reuse |
Why we can't keep the pool and the poolManager as a global pool ? then those annoying concurrent problem will gone once for all . |
You can. Create a static field to store it.
You can have any number of HttpClient instances that are all sitting on top of the same HttpClientHandler, e.g. private static readonly HttpClientHandler s_handler = new HttpClientHandler();
...
using (var hc = new HttpClient(s_handler, disposeHandler: false))
{
hc.BaseAddress = ...;
hc.DefaultRequestHeaders.Add(...);
... // make whatever requests here
} // the handler won't be disposed here, and it's actually what owns all the resources / the pool |
@stephentoub I wonder if you have 20 lib that access differenct network resouce (each lib have a configuration like this, but your app do not call those method) , Will there be more than 20 port in used but do nothing? if there is a new class HttpClient2:IDisposable
{
// add current HttpClient properties
// add current HttpClientHandler properties
public TimeSpan PooledConnectionTimeout { get; set;}
public static void ResetPooledConnectionLifetime() // reset dns of pool
// lifetime hook event
public Task BeforeSend(Action<HttpClient2,HttpRequestMessage> handler)
public Task AfterSend(Action<HttpClient2,HttpResponseMessage> handler)
public Task WhenException(Action<HttpClient2,Exception> handler)
// this is for retry
public Task<HttpResponseMessage> SendWithoutLifetimeHookAsync(HttpRequestMessage request,CancellationToken cancellationToken)
//
} and this can be a just a .net standar lib |
The connections in a pool don't stick around forever; they're cleaned up after a period of inactivity. And if you use |
So,What's your point ?
|
To use |
IMO, there must be a Ioc Container to use
Just curious, Is the |
Overall your original question seems to be answered, so closing. |
I know there some good thing happen on current
HttpClient
implementation and the newHttpClientFactory
.but the
HttpClientFactory
is not a good choice for "lib" development . and I'm not sure about the dispose behavior ofSocketsHttpHandler
.Will there be a new
HttpClient
( maybe calledHttpClientCng
orHttpClient2
) that design to work withusing
keyword?The text was updated successfully, but these errors were encountered: