Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RestSharp with async/await

I'm struggling to find a modern example of some asynchronous C# code that uses RestSharp with async and await. I know there's been a recent update by Haack but I don't know how to use the new methods.

Also, how can I provide a cancellation token so that the operation can be canceled (say, if a person is sick of waiting and presses the Cancel button in the app's UI).

like image 909
Pure.Krome Avatar asked Feb 14 '14 12:02

Pure.Krome


People also ask

Is RestSharp asynchronous?

The main purpose of RestSharp is to make synchronous and asynchronous calls to remote resources over HTTP.

Does RestSharp use HttpClient?

Since RestSharp uses the HttpClient, we should consider similar issues regarding the RestClient instantiation.

How do you call async method without await?

However, just to address "Call an async method in C# without await", you can execute the async method inside a Task. Run . This approach will wait until MyAsyncMethod finish. await asynchronously unwraps the Result of your task, whereas just using Result would block until the task had completed.

What is RestSharp?

RestSharp is a comprehensive, open-source HTTP client library that works with all kinds of DotNet technologies. It can be used to build robust applications by making it easy to interface with public APIs and quickly access data without the complexity of dealing with raw HTTP requests.


2 Answers

Well, the update Haack is referring to has been made by me :) So let me show you how to use it, as it is actually very simple. Previously you had methods like ExecuteAsyncGet that would return a RestSharp custom type named RestRequestAsyncHandle. This type could not be awaited as async/await works on Task and Task<T> return types. My pull-request added overloads to the existing async methods that return Task<T> instances. These Task<T> overloads have an added "Task" string added to their names, for example the Task<T> overload for ExecuteAsyncGet is called ExecuteGetTaskAsync<T>. For each of the new Task<T> overloads there is one method that does not require a CancellationToken to be specified and there is one that does.

So now on to an actual example on how to use it, which will also show how to use a CancellationToken:

private static async void Main() {     var client = new RestClient();     var request = new RestRequest("http://www.google.com");     var cancellationTokenSource = new CancellationTokenSource();      var restResponse =          await client.ExecuteTaskAsync(request, cancellationTokenSource.Token);      // Will output the HTML contents of the requested page     Console.WriteLine(restResponse.Content);  } 

This will use the ExecuteTaskAsync overload that returns a Task<IRestResponse> instance. As it returns a Task, you can use the await keyword on this method and get returned the Task<T>'s returned type (in this case IRestResponse).

You can find the code here: http://dotnetfiddle.net/tDtKbL

like image 144
Erik Schierboom Avatar answered Sep 20 '22 06:09

Erik Schierboom


In my case, I had to call Task.Wait() for it to work properly. However, I used the version which does not take CancellationTokenSource as parameter.

private static async void Main() {     var client = new RestClient();     var request = new RestRequest("http://www.google.com");     Task<IRestResponse> t = client.ExecuteTaskAsync(request);     t.Wait();     var restResponse = await t;     Console.WriteLine(restResponse.Content); // Will output the HTML contents of the requested page } 
like image 45
Ram Jayaraman Avatar answered Sep 21 '22 06:09

Ram Jayaraman