Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp Timeout not working

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?

like image 560
Miljan Vulovic Avatar asked Oct 05 '17 11:10

Miljan Vulovic


People also ask

What is Restrequest C#?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.

Does RestSharp reuse HttpClient?

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use.


3 Answers

Solution (Version 107+)

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

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
like image 63
BenKoshy Avatar answered Oct 17 '22 09:10

BenKoshy


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;
        }
like image 23
Ken Brittain Avatar answered Oct 17 '22 10:10

Ken Brittain


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

like image 5
Rami A. Avatar answered Oct 17 '22 08:10

Rami A.