RestSharp Timeout not working

Miljan Vulovic picture Miljan Vulovic · Oct 5, 2017 · Viewed 30.4k times · Source

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?

Answer

BKSpurgeon picture BKSpurgeon · Oct 11, 2018

How to alter the default time out period length:

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

That's it, and I hope that helps!