2
votes

I want to consume a rest api from my code. Now, for this, I can use WebClient or HttpClient.

However, HttpClient has connection pooling support, as mentioned in the documentation.

every HttpClient instance uses its own connection pool, isolating its requests from requests executed by other HttpClient instances

So, does WebClient has connection pooling support?

as I will be calling this api many a times, hence it do not want "creating new connections for every call" be a overhead for performance during api calls.

So which one should I be using, for better performance?

1
As far as I know, there is nothing like "connection pooling" in the HTTP world similar to "connection pooling" with databases. - Thangadurai
@PatrickHofman Thanks for the link. However, the main question still remains unanswered. Does WebClient has connection pooling? - RohitWagh
It does answer your "So which one should I be using" question. - Patrick Hofman

1 Answers

0
votes

I was searching around this to. And found some updated information HttpClient does have connection pools.

TLDR:

var socketHandler = new SocketsHttpHandler()
{
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
    PooledConnectionLifetime = TimeSpan.FromMinutes(10),
    PooledConnectionIdleTimeout = TimeSpan.FromMinutes(5),
    MaxConnectionsPerServer = 10
};

var httpClient = new HttpClient(socketHandler);

You can read more here: https://www.stevejgordon.co.uk/httpclient-connection-pooling-in-dotnet-core