Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
Should RestClient be singleton or new for every request

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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.

If you are wondering why this is the case, you can see explanation here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!