Search code examples
timeoutrestsharp

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."

  • Is this a timeout on the url's end or my httpclient?
  • Is request.timeout correct?

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");

Solution

  • 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 = 300000 });
    

    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.