in c#

Async and RESTSharp

tl;tr; Examples of using Async method of RESTSharp, and comments

As I mentioned last time I was forced to use async architecture because Windows Phone require it. I was newbie in async programming but when I met RESTSharp, it clear everything, I will show you some code block with my comment for it. Basics, and how to connect to Web API check this blogpost

If we want execute GET.

We have to remember that async method is runs in brand new thread! First we have to set REST request, type here your api path, and what method, for this case it will be GET. r.Data is deserialised List of Locations.

If we want execute POST

Here we fill body of our REST request, we add new object which will provide information that we want to POST.

If we want execute DELETE

Now we have to add parameter to URL so our request for example will be “http://example.com/api/location/1”, and method DELETE

If we want to update (PUT)

In this case we have to provide object which will replace old one, by using of AddObject method. And finally we can execute our request.

Most of you will ask me, how works this part. If we want to execute method ExecuteAsync<T>(). As a first argument we have to provide request. Second argument is CallBack – what we want to do when data will be received, so we can put here some MessageBox, event for logger or simply bind our given data.

In next episode we will be checking, is there any possibility to mix REST a XNA,

  • Bataleon

    Great article! Would you recommend we use async/await with RESTSharp now instead?

    • http://pawel.sawicz.eu/ Pawel Sawicz

      Yes absolutely, right now we can get Task so we can use async/await. If I will have more time I will try to type up something about it.

  • Conall Ó Gribín

    Hi Pawel. Thank you for such nice clear explanations. I just wanted to let you know that I found your post via a recommendation on stackoverflow, and i’m very grateful!

    I look forward to reading summore, especially about the hellbreath server. I used to run legend of mir 2 servers as a kid, so similar passion at the time 🙂

    • http://pawel.sawicz.eu/ Pawel Sawicz

      thanks! I will try to write more about helbreath 😉