RestSharp.RestClient ignores timeout

I am developing a .net application using RestSharp.RestClient (105.2.4-rc4-24214-01). I have installed

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 uses the default value of 100000 and generates "The task was canceled." exception only after 100000 ms.

How can I change this value?

+5
source share
1 answer

You can use the CancellationTokenSource and use its token to abort the response descriptor. Please try entering the code below 10 seconds.

 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 this helps!

0
source

Source: https://habr.com/ru/post/1268093/


All Articles