Maintaining a single instance to a RestSharp client

gregsdennis picture gregsdennis · Oct 16, 2014 · Viewed 12.5k times · Source

Along similar lines to this question, is it okay to instantiate a client and hold on to it, or do I need to create a new instance for every call (or batch of calls)?

Answer

Yuval Itzchakov picture Yuval Itzchakov · Oct 16, 2014

Whenever in doubt and if possible, look at the source code.

From a brief look it seems acceptable and even the better approach to hold on to a single instance, as it mainly responsible for executing IRestRequest requests.

I was once in doubt with the same question regarding HttpClient and found the following:

The default HttpClient is the simplest way in which you can start sending requests. A single HttpClient can be used to send as many HTTP requests as you want concurrently so in many scenarios you can just create one HttpClient and then use that for all your requests.

If concurrency is required, explore the source code to see if there may be any pitfalls. You can always default back to HttpClient which is a part of the BCL since .NET 4.5 (and can be installed via NuGet on .NET 4.0)