Experimental Async support for RestSharp

This article is outdated and based on my very first, experimental patch. John Shehan made some massive improvements, so I only leave this article online for reference about the traditional ASyncPattern. Have a look at RestClient.ExecuteAsync in newer versions of RestSharp.

I have just commited a change to my ASync branch on my fork of RestSharp on GitHub. I want to do some more testing first before I ask John Sheehan to pull in the change. The new files are IAsyncRestClient.cs and AsyncRestClient.cs which derive from their normal (synchronous) implementations. Edit: This change was merged into the official RestSharp source code and is documented on the RestSharp Wiki. There was a major bug fix to the functionality on April 18, 2010.

Anyway, here are the examples on how to use it. I assume you know what the state parameter on an Async call does, if not just set it to null.

var client = new AsyncRestClient(serviceUrl);
var request = new RestRequest(Method.GET);

// Synchronous Execution still works - AsyncRestClient derives from RestClient
var SyncResponse = client.Execute(request);
Console.WriteLine(response.Content);

// But Asynchronous Execution is much nicer!
// Method 1: Waiting for an Asynchronous Call with EndExecute
var asyncRes1 = client.BeginExecute(request, null, "some state, can be null");
var responseAsync1 = client.EndExecute(asyncRes1);
Console.WriteLine(responseAsync1.Content);

// Method 2: Polling for Asynchronous Call Completion
var asyncRes2 = client.BeginExecute(request, null, "some state");
while (!asyncRes2.IsCompleted)
{
    Console.Write(".");
    System.Threading.Thread.Sleep(100);
}
var responseAsync2 = client.EndExecute(asyncRes2);
Console.WriteLine(responseAsync2.Content);

// Method 3: Waiting for an Asynchronous Call with WaitHandle
var asyncRes3 = client.BeginExecute(request, null, "some state");
asyncRes3.AsyncWaitHandle.WaitOne();
var responseAsync3 = client.EndExecute(asyncRes3);
Console.WriteLine(responseAsync3.Content);

// Method 4: Using a Callback Method
static void Main(string[] args)
{
    var asyncRes4 = client.BeginExecute(request, EndResponse, "some state");
    Console.ReadLine();            
}

static void EndResponse(IAsyncResult res)
{
    var result = (AsyncResult)res;
    var caller = (RequestExecuteCaller)result.AsyncDelegate;
    var response = caller.EndInvoke(res);
    Console.WriteLine("AsyncResult: "+response.Content);
}

// This also works for the Execute<T> method, without callback...
var asyncResT = client.BeginExecute<MyDTOClass>(request, null, "some state");
var responseAsyncT = client.EndExecute<MyDTOClass>(asyncResT);
Console.WriteLine(responseAsyncT.Data.SomePropertyInMyClass);

// ...and with Callback
static void Main(string[] args)
{
    var asyncResTC = client.BeginExecute<MyDTOClass>(request, EndResponse, "some state");
    Console.ReadLine();            
}

static void EndResponse(IAsyncResult res)
{
    var result = (AsyncResult)res;
    var caller = (RequestExecuteCaller<MyDTOClass>)result.AsyncDelegate;
    var response = caller.EndInvoke(res);
    Console.WriteLine("AsyncResult: "+response.Data.SomePropertyInMyClass);
}

Comments (5)

John SheehanApril 2nd, 2010 at 16:18

This is great Michael! I'll hopefully play with it over the weekend and let you know if I encounter any issues.

Damian KarzonApril 19th, 2010 at 16:38

Love your work on this! I'll have to give it a try and give you some feedback.

ScottMarch 3rd, 2011 at 13:56

Hi, is this code still applicable, I have been looking for a IAsyncResult for RestSharp, but have not found anything (and the files you referenced in this blog do not appear to be in the RestSharp repository)

mstumMarch 4th, 2011 at 07:11

@Scott I don't think it is, John Sheehan made massive changes over the past year. However, RestClient has an ExecuteAsync method that takes an Action delegate. So it seems it's now much more simplified.