RestClient Timeout #13
Comments
Just use a CancellationTokenSource with timeout. var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var response = client.Execute(request, cts.Token); |
Thanks a lot 👍 |
BTW: When you need more control over the HttpClient, then you can still overload DefaultHttpClientFactory.CreateClient, call its base and modify its behavior. |
Cool! |
I'm not really sure what you mean. Do you have more details about this feature? |
Like in this function from the original Restsharp library:
|
Ah, now I understand. I thought about this too, but came to the conclusion that this isn't necessary for RestSharp.Portable, because it uses the TPC (async/await), which includes this feature anyway. var response = await client.Execute(request); you're also able to do the following: client.Execute(request)
.ContinueWith(t => {
var result = t.Result;
// do whatever you want with the result
}); Another main difference between RestSharp and RestSharp.Portable is that failed requests already throw an exception (except when explicitly disabled), so there's no reason to manually check for the status code. |
The thing is that I want to isolate my communication layer as it's shared between different mono, windows and windows phone projects. so the callbacks should be passed from outside. So I can execute your provided code (with my callback) on success and in a catch block, I can execute my failure callback; like the following:
What do you think? |
This is possible, but why don't you use RestSharp.Portable with async/await directly (or indirectly through a custom communication client), without the need to provide callbacks? The callback handler stuff seems to be overly complex. BTW: The error handling should be done in the following way: var execTask = client.Execute(request);
execTask
.ContinueWith(t => {
var result = t.Result;
// do whatever you want with the result
}, TaskContinuationOptions.OnlyOnRanToCompletion);
execTask
.ContinueWith(t => {
var exception = t.Exception;
// do whatever you want with the exception
}, TaskContinuationOptions.OnlyOnFaulted);
execTask
.ContinueWith(t => {
// do whatever you want with the cancellation
}, TaskContinuationOptions.OnlyOnCanceled); Be aware, that when you use ContinueWith or async/await, that the code inside the ContinueWIth or after the await might be executed in a different thread. |
Thank a lot, much appreciated :) |
Hi ! I allow myself to write here because it seems to be a continuation of the subject : strangely, your snippet doesn't seem to work like expected. Here is what I do to get things working (within try-catch block).
Am I missing something ? Thank you to enlighten me. =) |
Fear not! The new "Timeout" property comes to the rescue 😀 . Just update to the version 2.4.0 and you don't have to fiddle with "CancelAfter" anymore. Be aware, that the timeout is only available as IRestClient property, because the HttpClient instance is kept alive as long as the RestClient instance is alive. |
Even better, thanks a lot ! \o/ Having said, be careful that your snippet on 14 Jan (which is probably what most people looking for timeout spec gonna see) is not correct. ;) |
What is the unit for the Timeout property? Seconds? Minutes? |
"Timeout in milliseconds to use for requests made by this client instance" |
Hello,
Is there a way to set timeout to the rest client?
Thanks
The text was updated successfully, but these errors were encountered: