Meneghini
Meneghini

Reputation: 187

RestSharp Timeout

I have an issue with RestClient response is coming back as

"StatusCode: 0, Content-Type: , Content-Length: )" with the ErrorMessage of "The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing."

It might take 5+ minutes for this request even though it's only 170KB of data due to their end being poorly optimized.

var client = new RestClient(url);

RestRequest request = new RestRequest() { Method = Method.Get };

request.Timeout = 300000;

request.AddParameter("access_token", AccessToken);
request.AddParameter("start_date", StartDate.ToString("yyyy-MM-dd"));
request.AddParameter("end_date", EndDate.ToString("yyyy-MM-dd"));
request.AddParameter("offset", offset.ToString());

var response = await client.ExecuteAsync(request);
var responseWorkLoads = JObject.Parse(response.Content).SelectToken("worklogs");

Upvotes: 12

Views: 31259

Answers (2)

Alexey Zimarev
Alexey Zimarev

Reputation: 19630

There are two timeouts that RestSharp allows you to set.

When you create a new instance of RestClient, you can specify the HttpClient timeout that will override the default 100 ms using RestOptions:

var client = new RestClient(
    new RestClientOptions { Timeout = TimeSpan.FromMinutes(5) }
);

As the wrapped HttpClient is instantiated and configured once per RestClient instance, setting the request timeout doesn't override that setting, otherwise the client won't be thread-safe.

The request timeout, on the other hand, overrides the client timeout if it is less than the client timeout. RestSharp creates a cancellation token source using the request timeout, so the request will be cancelled when the linked cancellation token cancels.

I believe that currently RestClient also doesn't set the failure reason properly when the client times out, only if the token gets cancelled. I will create an issue for that.

Upvotes: 26

VirajWijesooriya
VirajWijesooriya

Reputation: 41

The above answer worked for me too. This is how I modified the constructor to create the RestClient with the BaseUrl and the timeout.

I'm using RestSharp 110.2.0 and it looks like the field is now called MaxTimeOut.

var options = new RestClientOptions($"{BaseUrl}");
options.MaxTimeout = 300000;

RestClient restClient = new RestClient(options);

Upvotes: 4

Related Questions