Can I use persistent connections with System.Net.Http.HttpClient?

I created a REST resource using the web API running as a standalone process. For performance reasons, I would like to be able to call it using persistent HTTP connections. I am using OWIN standalone hosting.

I really like asnyc methods for GET, POST, PUT, DELETE in System.Net.Http.HttpClient. They are easy to call and handle - they return System.Threading.Tasks.Task, which is convenient for what I'm trying to do. I prefer to use HttpClient for System.Net.HttpWebRequest.

I probably missed something, but it's not easy for me to figure out how to create persistent connections to HttpClient. I am digging the classes System.Net.Http.HttpClientHandler and System.Net.Http.WebRequestHandler, but so far I have not found the possibility for persistent connections. Google finds all kinds of examples of creating persistent connections using HttpWebRequest. It has a KeepAlive property, which can be set to true. Is there a way to set this using HttpClient?

MSDN Documentation for HttpClient :

By default, HttpWebRequest will be used to send requests to the server. This behavior can be changed by specifying a different channel in one of the constructor overloads using the HttpMessageHandler instance as a parameter. If authentication or caching functions are required, WebRequestHandler can be used to configure parameters, and an instance can be passed to the constructor. The returned handler can be passed to one of the constructor overloads with the HttpMessageHandler parameter.

Is there a way to set the KeepAlive function in a basic HttpWebRequest?

The MSDN documentation also says:

An instance of the HttpClient class acts as a session to send HTTP requests. An HttpClient instance is a set of parameters that apply to all requests made by this instance. In addition, each HttpClient instance uses its own connection pool, isolating its requests from requests made by other HttpClient instances.

Can I understand that the connection pool will optimize the use of persistent connections for me when you can get the performance benefit? What if I want there to be only one connection with my client?

+8
c # rest web-services asp.net-web-api
source share
3 answers

I'm not sure what exactly you are trying to solve, but have you looked at SignalR? They have a pretty attractive websocket api that can do the work you are looking for.

If you are not using .NET 4.5, they have alternative mechanisms. One of the elements I use is something like a server / event stream.

+1
source

You will need to set the maximum MaxIdle time for ServicePoint for the client. The easiest way is to set a timeout for all service points:

ServicePointManager.MaxServicePointIdleTime = Timeout.Infinite; 

You can also install it when connected to a specific endpoint

 var sp = ServicePointManager.FindServicePoint(targetUri); sp.MasIdleTime = Timeout.Infinite; 
+1
source

Maybe try adding a Keep-Alive header?

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Keep-Alive

 HttpClient _httpClient = new HttpClient(); _httpClient.DefaultRequestHeaders.Add("Keep-Alive", "timeout=5, max=1000"); 
0
source

All Articles