13

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

Jeremy Paskali
  • 140
  • 2
  • 8
gregsdennis
  • 7,218
  • 3
  • 38
  • 71

4 Answers4

26

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)

taha
  • 722
  • 7
  • 15
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 1
    Be aware that for failover scenarios that rely on short TTL DNS (like Azure TrafficManager), the Singleton approach is flawed, even though generally it is accepted as best practice to reuse for all requests. You can see discussion at the links included. https://github.com/dotnet/corefx/issues/11224 and http://www.nimaara.com/2016/11/01/beware-of-the-net-httpclient/ – pseabury Jun 19 '18 at 15:55
  • @pseabury I think they resolved it in .NET Core 2.1? At least I remember them talking about it. – Yuval Itzchakov Jun 19 '18 at 15:59
  • 2
    Yes, mostly. From the end of the Github thread I ref'd "AFAIK it does not work either on .NET Core. In .NET Core 2.1 we introduced SocketsHttpHandler.PooledConnectionLifetime. Also the new HttpClientFactory in ASP.NET can leverage both and does even more for you." But, not everyone might be able to use .NETCore 2.1 – pseabury Jun 19 '18 at 16:07
0

Though this post seems quite old but i landed here and would like to make some corrections about the actual question. Q. is it okay to instantiate a client and hold on to it, or do I need to create a new instance for every call A. No you dont need to recreate on each request, same applies to RestClient or HttpClient, In case of RestClient you can even use the same instance of RestRequest, just update the body like: request.AddOrUpdateParameter("application/json", myJsonObject, RestSharp.ParameterType.RequestBody);

Altaf
  • 189
  • 2
  • 4
-6

As long as you need to call the same server to make Rest Requests, it is ok to have only one RestClient and use that same client to make multiple RestRequest.

Example C# Code:

var client = new RestClient("url here"))

// First Call
var request = new RestRequest("API/Path", Method.POST);
request.AddParameter("parameter", "value");
request.AddHeader("header", "value");
var response = client.Execute(request);


// Second Call
var request2 = new RestRequest("API/Path", Method.POST);
request2.AddParameter("parameter", "value");
request2.AddHeader("header", "value");
var response2 = client.Execute(request2);

Notice here, the client variable. Which I have used twice because it is the base point for both the requests. No point in duplicating it for every request.

Hope this helps.

Gaspa79
  • 5,488
  • 4
  • 40
  • 63
Basit Nizami
  • 345
  • 4
  • 14
  • Are there any long term issues I should be aware of, like memory leaks or lingering open connections? – gregsdennis Oct 16 '14 at 20:34
  • well, you can always dispose it when not needed. there shouldn't be any issues as far as I can tell. To be on a safer side, how about if you go with the `using` block. – Basit Nizami Oct 16 '14 at 21:00
  • 11
    `RestClient` doesn't implement `IDisposable`. If it did and the OP used it, he wouldn't be holding a single instance. He would have to initialize a new one for each call he makes. – Yuval Itzchakov Oct 17 '14 at 06:36
  • The using gave me the direction I needed. Although IDisposable isn't implemented, I feel comfortable instantiating a new client per batch of calls. It's a happy medium. – gregsdennis Oct 17 '14 at 16:35
  • 4
    This code won't even compile due to RestClient not implementing IDisposable. Also the answer shows only that it's *possible* to reuse the instance and nothing about whether it's wise to do so. – EagleBeak Jul 03 '15 at 07:58
  • 5
    Why is this the accepted answer if `RestSharp` doesn't implement `IDisposable`? – Rahul Kishore Nov 10 '16 at 07:00
  • This is not an acceptable response .. RestSharp doesn't implement IDisposable. – KJBTech Aug 28 '17 at 15:09
  • Just submitted an edit to remove the using syntactic sugar for future readers – Gaspa79 Jun 19 '18 at 15:21
-8

Try:

var client = new RestClient("http://server/");
client.CookieContainer = new System.Net.CookieContainer();

Source: https://github.com/restsharp/RestSharp/wiki/Cookies

Pang
  • 9,564
  • 146
  • 81
  • 122