DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Async and RestSharp for Windows Phone 7

Toni Petrina user avatar by
Toni Petrina
·
Oct. 03, 12 · Interview
Like (0)
Save
Tweet
Share
5.69K Views

Join the DZone community and get the full member experience.

Join For Free

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.


Windows Phone

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Fraud Detection With Apache Kafka, KSQL, and Apache Flink
  • Mr. Over, the Engineer [Comic]
  • 2023 Software Testing Trends: A Look Ahead at the Industry's Future
  • Educating the Next Generation of Cloud Engineers With Google Cloud

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: