Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 861
Timur Lemeshko Avatar asked May 22 '17 09:05

Timur Lemeshko


People also ask

What is the default timeout for RestClient?

The default value of the REST client response timeout is 120 seconds.

Should RestClient be singleton or new for every request?

RestSharp does not use connection pool as HttpClient and does not leave opened sockets after the use. That's why it is safe (and recommended) to create a new instance of RestClient per request.

Does RestSharp work with .NET core?

RestSharp supports both synchronous and asynchronous requests. This article presents a discussion of how we can work with RestSharp to consume services built using ASP.NET Core.

What is Restrequest C#?

RestSharp is a C# library used to build and send API requests, and interpret the responses. It is used as part of the C#Bot API testing framework to build the requests, send them to the server, and interpret the responses so assertions can be made.


1 Answers

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;
like image 161
MikeH Avatar answered Sep 30 '22 11:09

MikeH