Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining a single instance to a RestSharp client

Tags:

c#

restsharp

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)?

like image 281
gregsdennis Avatar asked Oct 16 '14 20:10

gregsdennis


1 Answers

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)

like image 70
Yuval Itzchakov Avatar answered Oct 27 '22 05:10

Yuval Itzchakov