ASP.Net HttpClient
is disposable, and a lot of articles say you should use the singleton pattern to use it because of the performance. But when I see the RestClient
it can't be disposed, and in the Recommended-Usage page the sample will new
the RestClient
every time. Should I use singleton pattern for RestClient
or should I new
it every time? If I new
it every time will there be any performance concern?
RestSharp GitHub
Some references:
Do HttpClient and HttpClientHandler have to be disposed
YOU'RE USING HTTPCLIENT WRONG AND IT IS DESTABILIZING YOUR SOFTWARE
HTTP client is a client that is able to send a request to and get a response from the server in HTTP format. REST client is a client that is designed to use a service from a server and this service is RESTful.
Recommended usageRestClient should be thread-safe. It holds an instance of HttpClient and HttpMessageHandler inside. Do not instantiate the client for a single call, otherwise you get issues with hanging connections and connection pooling won't be possible.
The main conclusion is that one is not better than the other, and we shouldn't compare them since RestSharp is a wrapper around HttpClient. The decision between using one of the two tools depends on the use case and the situation.
It's better to specify the Json data body. var client = new RestClient("URL"); var request = new RestRequest("Resources",Method. POST); request. RequestFormat = RestSharp.
should I use singleton pattern for RestClient or should I new it everytime, if I new it everytime will any performance concern?
Recommended way to use RestSharp
is to create a new instance per request.
It differs from Singleton approach recommended for HttpClient
. And the reason is that under the hood RestSharp
uses HttpWebRequest
for HTTP interaction, not HttpClient
. That's why the usage model differs.
If I create it everytime do I get performance issue just like the HttpClient?
The main reason why you shouldn't create a new instance of HttpClient
for each request is not a performance consideration. The time spent for creation and initialization will take a tiny fraction of time spent for following network call. The main reason to use singleton instance of HttpClient
is the following:
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads. This will result in SocketException errors.
RestSharp
does not use connection pool as HttpClient
and does not leave opened sockets after the use. That's why it is safe (and recommended) to create a new instance of RestClient
per request.
Will you gain any performance improvement if you use reuse instance of RestClient
? Well, you will save the time for creation of object and its initialization. However this time is very close to 0
and moreover it's a tiny fraction of time spent for following network call. You don't reuse other .NET objects like List<T>
because of performance considerations, are you? You should do the same for RestClient
. It's just developed in a way that implies such usage scenario.
From version v107
you should create only one instance. This version is using HttpClient
internally.
Do not instantiate RestClient for each HTTP call. RestSharp creates a new instance of HttpClient internally, and you will get lots of hanging connections, and eventually exhaust the connection pool.
If you use a dependency-injection container, register your API client as a singleton.
RestClient lifecycle
If you are wondering why this is the case, you can see explanation here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With