34

I have a restsharp client and request set up like this:

var request = new RestRequest();
request.Method = Method.POST;
request.AddParameter("application/json", jsonBody, ParameterType.RequestBody);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
request.Timeout = -1;
request.ReadWriteTimeout = -1;
var url = $"http://{ipAddress}/api/calculate";
var client = new RestClient();
client.BaseUrl = new Uri(url);
client.Timeout = -1;
client.ReadWriteTimeout = -1;
var response = client.Execute(request);

This request is going to take a while to finish, around 30 minutes. Now, I know that there are more elegant ways of doing this, but, for this request, I need to do it like this.

This RestSharp client and request are executed inside Windows service. When service executes request, it throws TimoutException and request maximum timeout is around 40 seconds.

For some reason, timeout that I set is not working for this case.

Anybody has a solution for this?

imsome1
  • 1,182
  • 4
  • 22
  • 37
Miljan Vulovic
  • 1,586
  • 3
  • 20
  • 48
  • 4
    Request should not take 30 minutes - you should rework your architecture and do it with some async method. – Jaroslav Štreit Nov 19 '18 at 11:19
  • 1
    I agree with @JaroslavŠtreit. Probably it would be a much better fit to return an "201 - Accepted" response to this request, and instruct the client caller application to check again, in another endpoint/url, if the processing is already done. That way the client application wouldn't need to be blocked waiting for the long operation to finish. – Ulysses Alves Jul 01 '20 at 14:06

3 Answers3

50

Solution (Version 107+)

var options = new RestClientOptions("baseURL") {
    ThrowOnAnyError = true,
    Timeout = 1000  // 1 second - thanks to @JohnMc
};
var client = new RestClient(options);

Older Versions:

to alter the default time out to: 5 seconds - for example - (i.e. 5000 milliseconds):

    var client = new RestClient("BaseUrl");
    client.Timeout = 5000; // 5000 milliseconds == 5 seconds
BenKoshy
  • 33,477
  • 14
  • 111
  • 80
  • 1
    FYI, the way to do this has changed in version 107. You set timeout on RestClientOptions and use that for the constructor of RestClient – John Mc May 18 '22 at 16:04
  • @JohnMc - thx i updated the code snippet to the latest version. – BenKoshy May 19 '22 at 02:50
26

You may not be doing what you think by setting the ReadWriteTimeout value. Your value is ignored so you get the default.

According to this answer What is default timeout value of RestSharp RestClient? RestSharp uses HttpWebRequest in its implementation.

The timeout property for HttpWebRequest cannot be negative HttpWebRequest.Timeout Property.

If you look in the RestSharp client code you see this: https://github.com/restsharp/RestSharp/blob/70de357b0b9dfc3926c95d1e69967c7a7cbe874c/RestSharp/RestClient.cs#L452

        int readWriteTimeout = request.ReadWriteTimeout > 0
            ? request.ReadWriteTimeout
            : this.ReadWriteTimeout;

        if (readWriteTimeout > 0)
        {
            http.ReadWriteTimeout = readWriteTimeout;
        }
Gregory
  • 6,514
  • 4
  • 28
  • 26
Ken Brittain
  • 2,255
  • 17
  • 21
  • Thank you for the answer. So, what do you suggest I do to put unlimited time for timeout? – Miljan Vulovic Oct 05 '17 at 12:10
  • 2
    First, I would break the operation into start, status, and result operations. Make the call, return a token, and then check status. Finally, get the saved result. If that is not feasible then I would suggest making the timeout as long as you can and hope for the best. If you control the API then go for what behind door #1. – Ken Brittain Oct 05 '17 at 13:55
  • 1
    You were right. In the official documentation, there is a -1 for unlimited timeout. I put it like Int32.MaxValue and that worked. Thank you mate! – Miljan Vulovic Oct 06 '17 at 12:39
5

Update to RestSharp v106.2.2.
See https://github.com/restsharp/RestSharp/issues/1093

Rami A.
  • 10,302
  • 4
  • 44
  • 87