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?
petrsnd
source share