It seems like a compelling blog post. However, before making a decision, I would first run the same tests that the blog writer ran, but on your own code. I would also try and find out a bit more about HttpClient and its behavior.states:An HttpClient instance is a collection of settings applied to all requests executed by that instance. In addition, every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances.So what is probably happening when an HttpClient is shared is that the connections are being reused, which is fine if you don't require persistent connections. The only way you're going to know for sure whether or not this matters for your situation is to run your own performance tests.If you dig, you'll find several other resources that address this issue (including a Microsoft Best Practices article), so it's probably a good idea to implement anyway (with some precautions). There is no one way to use the HttpClient class.
Jul 18, 2018 HttpClient GetAsync, PostAsync, SendAsync in C# July 18, 2018 1 Comment. HttpClient is a library in the Microsoft.NET framework 4+ that is used for GET and POST requests. Let’s go through a simple example of using HttpClient to GET and POST JSON from a web application.
The key is to architect your application in a way that makes sense for its environment and constraints.HTTP is a great protocol to use when you need to expose public API's. It can also be used effectively for light weight low latency Internal services - although the RPC message queue pattern is often a better choice for internal services.There is a lot of complexity in doing HTTP well.Consider the following:. Creating a socket and establishing a TCP connection uses network bandwidth and time. HTTP/1.1 supports pipeline requests on the same socket. Sending multiple requests one after the other, without needing to wait for the previous responses - this is probably responsible for the speed improvement reported by the Blog post. Caching and load balancer - if you have a load balancer in front of the servers, then ensuring your requests have appropriate cache headers can reduce the load on your servers, and get the responses to the clients quicker. Don't ever poll a resource, use HTTP chunking to return periodic responses.But above all, test, measure and confirm.
If it's not behaving as designed, then we can answer specific questions about how to achieve your expected results. @MichaelShaw: HttpClient implements IDisposable. It is therefore not unreasonable to expect it to be a short-lived object that knows how to clean up after itself, suitable for wrapping in a using statement each time you need it. Unfortunately, that's not how it actually works.
The blog post that the OP linked clearly demonstrates that there are resources (specifically, TCP socket connections) that live on long after the using statement has gone out of scope and the HttpClient object has presumably been disposed.–Sep 7 '16 at 23:05. I understand that thought process. It's just if you were thinking about HTTP from the architecture point of view, and was intending to make lots of requests to the same service - then you would be thinking about caching and pipelining, and then the thought of making HttpClient a short lived object would simply feel wrong.
Likewise, if you are making requests to different servers and would obtain no benefit from keeping the socket live, then disposing of the HttpClient object after its use makes sense.–Sep 8 '16 at 6:00.