If you’ve been following along with the Conference Buddy series, you have seen that Jeff Fritz has developed a back-end WebAPI service to provide the Conference Buddy clients with conference data. This blog post will walk you through consuming this service to obtain Event data in JSON format for our Conference Buddy application.
It is quite possible these days that some data will need to be consumed and provided by many different types of clients. These clients may consist of a mixture of mobile phones, web browsers, tablets and desktop computers. Conference Buddy falls into this category as it is being made available on the Windows Phone, Windows 8 Tablets as well as on the Windows desktop. An effective way to provide common data once, without resorting to duplication is to create a service layer over the data being provided. One type of service is called the RESTful service, it is meant to be lightweight and simple to use by issuing common HTTP verbs to interact with the data.
ASP.NET WebAPI is a framework that provides a RESTful interface to your data. ASP.NET WebAPI gives clients the option of communicating data through XML or through JSON. In the case of Conference Buddy, the team of Evangelists have chosen to use JSON as it is more terse, and therefore more compact and issues smaller payloads across the wire. It also takes up less space when storing data locally to account for disconnected client scenarios. JSON data is also simple to work with as you will discover in this blog post.
The first thing we will do is add a new C# class library project to our Conference Buddy solution, I’ve named the project “ConferenceBuddyLib”. Delete the Class1.cs class that is generated by default with the project template.
Now that we have the project setup, we will use the Library Package Manager (NuGet) to pull down a couple very useful libraries into the ConferenceBuddyLib project, these are:
Access NuGet by right-clicking on the ConferenceBuddyLib project and selecting “Manage NuGet Packages”, ensure the Online/NuGet official package source is selected and search for Json.NET, then click the Install button next to its entry.
Similarly, you will now search for HTTP Client, and click the Install button next to the Microsoft .NET Framework 4 HTTP Client Libraries.
The next thing that we will need is an Event model that coincides with the JSON being returned by the Conference Buddy WebAPI service. Add a new class to the ConferenceBuddyLib project called Event.cs and define it as follows:
Now we will need a repository class that will be responsible for obtaining event data from the service, parsing and deserializing the JSON from the service and into our Event class. Add a new class to the ConferenceBuddyLib project called JsonEventsRepository.cs and define it as follows:
In order to use the Events repository in our Conference Buddy Client application, we will need to add a solution reference to the class library that we just created. For now, let’s be sure that our call out to the service works. Open the code view of CoreForm and in the load event, add the following code to initialize our Events repository and make the call out to the Conference Buddy WebAPI service and obtain a list of events. Finally we will display a message box to verify that data has indeed been retrieved from the service:
In this blog post we added a class library to the Conference Buddy solution to assist us in retrieving data from the ASP.NET WebAPI back-end web service. We saw how easy it was to call and consume this service using the Json.NET and .NET HTTP Client libraries obtained from NuGet, and we verified the retrieval of data by displaying a message box in our Conference Buddy client application.
In the next blog post, we will put the Event data to good use and display it in our application using Live Tiles and RadPanorama.
Please note the source code download contains a JSON file with Event information as you may or may not have the Conference Buddy WebAPI services running on your machine, the ability to read JSON from a file has also been provided in the JsonEventsRepository class.
Subscribe to be the first to get our expert-written articles and tutorials for developers!