Skip to content
This repository has been archived by the owner on Apr 10, 2018. It is now read-only.

RestClient Timeout #13

Closed
sohayb opened this issue Jan 14, 2015 · 15 comments
Closed

RestClient Timeout #13

sohayb opened this issue Jan 14, 2015 · 15 comments
Assignees
Labels

Comments

@sohayb
Copy link

sohayb commented Jan 14, 2015

Hello,
Is there a way to set timeout to the rest client?
Thanks

@fubar-coder fubar-coder self-assigned this Jan 14, 2015
@fubar-coder
Copy link
Contributor

Just use a CancellationTokenSource with timeout.

var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10));
var response = client.Execute(request, cts.Token);

@sohayb
Copy link
Author

sohayb commented Jan 14, 2015

Thanks a lot 👍

@fubar-coder
Copy link
Contributor

BTW: When you need more control over the HttpClient, then you can still overload DefaultHttpClientFactory.CreateClient, call its base and modify its behavior.

@sohayb
Copy link
Author

sohayb commented Jan 14, 2015

Cool!
Are you planning to add Actions (callbacks) with the async execution of requests in the future?

@fubar-coder
Copy link
Contributor

I'm not really sure what you mean. Do you have more details about this feature?

@sohayb
Copy link
Author

sohayb commented Jan 14, 2015

Like in this function from the original Restsharp library:
RestRequestAsyncHandle ExecuteAsync<T>(IRestRequest request, Action<IRestResponse<T>, RestRequestAsyncHandle> callback);
by then I can execute requests like this, with erroCallBack is Action and successCallback is Action

    client.ExecuteAsync(request, response = > {
    if (forceError) {
        errorCallBack("Failed");
    } else {
        if (response.StatusCode == HttpStatusCode.OK) {
            successCallback();
        } else {
            errorCallBack("Failed");
        }
    }
 });

@fubar-coder
Copy link
Contributor

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.
Instead of

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.

@sohayb
Copy link
Author

sohayb commented Jan 14, 2015

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:

            try
            {
                _client.Execute<AppointmentResponse>(request).ContinueWith(t =>
                {
                    var result = t.Result;
                    successCallback((AppointmentResponse)result);
                });
            }
            catch(Exception ex)
            {
                failureCallback(ex.Message);
            }

What do you think?

@fubar-coder
Copy link
Contributor

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.

@sohayb
Copy link
Author

sohayb commented Jan 14, 2015

Thank a lot, much appreciated :)

@simondebaecke
Copy link

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

CancellationTokenSource cts = new CancellationTokenSource ();
cts.CancelAfter (TimeSpan.FromSeconds (10));
response = await client.Execute<T> (request, cts.Token);

Am I missing something ? Thank you to enlighten me. =)

@fubar-coder
Copy link
Contributor

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.

@simondebaecke
Copy link

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

@vdevappa
Copy link

vdevappa commented Nov 8, 2017

What is the unit for the Timeout property? Seconds? Minutes?

@rubemalmeida
Copy link

"Timeout in milliseconds to use for requests made by this client instance"
ref: http://www.nudoq.org/#!/Packages/RestSharp/RestSharp/RestClient

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants