Mobile Zone is brought to you in partnership with:

My name is Toni Petrina and I am a software developer and an occasional speaker. Although I primarily develop on the Microsoft stack, I like to learn new technologies. My hobbyist projects range from game development, regardless of the technology, to ALM. I spend most of my time with my girlfriend and someday I will learn how to play the guitar properly. Toni is a DZone MVB and is not an employee of DZone and has posted 69 posts at DZone. You can read more from them at their website. View Full User Profile

Async and RestSharp for Windows Phone 7

10.03.2012
| 3440 views |
  • submit to reddit

I have mentioned RestSharp quite a lot of times, it is simply an essential library for building applications that communicate to the outside world. Ever since Async CTP shipped for Windows Phone 7, I wanted to make everything asynchronous. RestSharp comes with asynchronous methods already built in, but they are not compatible with Async since they don’t return Task.

But we can fix that easily with our good friend TaskCompletionSource<T>

The idea is simple: create a TaskCompletionSource instance which will capture our asynchronous task and use regular ExecuteAsync method. Once it completes, it will transition the task to either Faulted state by setting the exception or to Completed state by setting the result.

Let’s convert ExecuteAsync method first:

public static Task<IRestResponse> ExecuteTaskAsync(this RestClient @this, RestRequest request)
{
    if (@this == null)
        throw new NullReferenceException();

    var tcs = new TaskCompletionSource<IRestResponse>();

    @this.ExecuteAsync(request, (response) =>
    {
        if (response.ErrorException != null)
            tcs.TrySetException(response.ErrorException);
        else
            tcs.TrySetResult(response);
    });

    return tcs.Task;
}

Looks good enough, what about generic method ExecuteAsync<T> that deserializes the content? Here we have a dilemma: what should the return type be? I have decided to return deserialized instance of type T i.e. theresponse.Data property directly instead of the full response, but you can change the code easily if you want to return the response instead. Here is the snippet:

public static Task<T> ExecuteTaskAsync<T>(this RestClient @this, RestRequest request)
    where T : new()
{
    if (@this == null)
        throw new NullReferenceException();

    var tcs = new TaskCompletionSource<T>();

    @this.ExecuteAsync<T>(request, (response) =>
    {
        if (response.ErrorException != null)
            tcs.TrySetException(response.ErrorException);
        else
            tcs.TrySetResult(response.Data);
    });

    return tcs.Task;
}

You can now use await method with RestSharp libraries. Happy coding.


Published at DZone with permission of Toni Petrina, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)