Timur Lemeshko
Timur Lemeshko

Reputation: 2879

RestSharp.RestClient ignores timeout

I'm developing a .net core application using RestSharp.RestClient (105.2.4-rc4-24214-01). I set

RestClient.Timeout=1 

or

RestClient.Timeout=10000

and then call my test API

var tcs = new TaskCompletionSource<IRestResponse>();
RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
return tcs.Task.Result;

But it still use default value 100000 and generate "A task was canceled." exception only after 100000 ms.

How can i change this value?

Upvotes: 7

Views: 7595

Answers (2)

MikeH
MikeH

Reputation: 21

The documentation says the Request.Timeout overrides the RestClient.Timeout. Try this:

var tcs = new TaskCompletionSource<IRestResponse>();
request.Timeout = 10000;
RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
return tcs.Task.Result;

Upvotes: 2

Vikas Pandey
Vikas Pandey

Reputation: 1

You can use CancellationTokenSource and use its token to abort the handle to response. Please try below code to put a 10 second timeout

System.Threading.CancellationTokenSource cts = new  System.Threading.CancellationTokenSource ();
 cts.CancelAfter (TimeSpan.FromMilliseconds(10000));
var tcs = new TaskCompletionSource<IRestResponse>();
 RestRequestAsyncHandle handle = RestClient.ExecuteAsync(request, response => { tcs.SetResult(response); })
 using (cts.Token.Register(() => handle.Abort()))
        {
        return (RestResponse)(await tcs.Task);
        }

Hope it helps!!

Upvotes: 0

Related Questions