Consuming REST Services in Windows Phone 7 Applications



Windows Phone 7 is a mobile operating system developed by Microsoft, and is the successor to its Windows Mobile platform. It is primarily aimed at the consumer market rather than the enterprise market. Here, we will focus on developing for Windows Phone 7. The Windows Phone Application Platform enables developers to create engaging consumer experiences running on a Windows Phone. It is built upon existing Microsoft tools and technologies such as Visual Studio, Expression Blend, Silverlight, and the XNA Framework. Developers already familiar with those for Windows Phone without a steep learning curve.  


REST is an architectural style for building distributed hypermedia-driven applications. It involves building resource-oriented services by defining resources that implement uniform interfaces using standard HTTP verbs (GET, POST, PUT, and DELETE), and that can be located or identified by an URI. 

In this article, let's develop a simple application that accesses employee information using WCF and REST services. With the .NET Framework 3.5 release, WCF added support for building REST style services.

We will use Microsoft Visual Studio 2010 and Windows Phone Developers tools to create the sample application.

1. First, let's navigate to Visual Studio 2010 and create a new WCF Service application. To do this, navigate to File->New->Project->WCF Service Application.

2. Next, open the IService1.cs file to create the Service contract and the Data contract. A service contract specifies the data types, location, and protocols used for the operations. A data contract is the description of a set of fields with a name and data type for each field.

3. Now, let's create a class named Employee and set its attributes to DataContract. This class has two data members - Employee ID and Employee Name. The attribute of these members are set as DataMember.

Pic1.png

4. Then, in the interface, create an Operation contract for getting the information of all the employees. Let's name it GetAllEmployees and it returns an array of employees. Also, set the WebGetAttribute. The WebGet attribute exposes operations using the GET verb. Now, set the RequestFormat and ResponseFormat as XML.

Pic2.png

5. Next, let's implement the GetAllEmployees method. To do this, navigate to the Service1.svc.cs file. In this, define the GetAllEmployees() method and return an array of Employees.

Pic3.png

6. Now, open the Service1.svc file. To do this, right-click Service1 in the Solution Explorer,and then, click View MarkUp.

7. After this, add an attribute named Factory to the Service1.svc file and set its value as System.ServiceModel.Activation.WebServiceHostFactory. The WebServiceHostFactory is a new class introduced in WCF 3.5 purely for making the WCF services restful. By using this class, we don't need any WCF configuration in our Web.config file.

Pic4.png
8. Now, let's view this service in the browser. To do this, right-click the application and then click View in Browser.



9. Next, let's create a Windows Phone 7 application that will consume the data of the WCF service created earlier.

10. To do this, navigate to File->New Project->Windows Phone 7 Application template.

Pic6.png

11. Now, add a reference to the System.Xml.Linq namespace to access the various XML classes. To do this, right-click the application in Solution Explorer, click Add Reference, and then, click System.Xml.Linq.



12. Then, in the MainPage.xaml file, add a few rows in the Grid named ContentGrid.


13. After this, add a Button, a TextBlock, and a ListBox to this Grid.

Pic8.png

14. Now, navigate to the MainPage.xaml class file and add a reference to the System.Xml.Linq namespace with the using statement.

Pic9.png
15. Then, in the constructor, register a click event handler of the button.

16. Next, let's handle the click event of the Button. In this, create an object of the WebClient class that provides the common methods for sending the data to and receiving the data from a resource identified by an URI.

17. After this, create an object of the string class that stores the URI of the WCF service created earlier.

18. Now, create an OpenReadCompleted event handler. This event occurs when an asynchronous operation to open a stream containing a resource completes.

19. Then, call the OpenreadAsynch() method and also pass the URI of the WCf service. This method opens a readable stream containing the specified resource.

Pic10.png

20. Now, let's handle the OpenReadCompleted event handler. In this, create an object of the xElement class. This class represents an XML element.

21. Then, using the OpenReadCompletedEventArgs class object, check if some error occurred.  If it did, then then simply return.

22. If no error has occurred, then call the Load() method to get the result in the form of XML.

23. Next, create a list of the XNode class object and get the list of nodes in the resultant xml document.

24. Also, create a List to store the information of Employees.

25. After this, start a foreach loop and get the Employee ID and Employee Name from each node in the XML document.

26. Then, set the ItemSource property of the ListBox to this list.

Pic11.png

27. Now, run the application. Next, click the button to call the service and get the employee information. A list of the Employee Id and Names is displayed.

This is a simple application that demonstrates how to develop a Windows Phone 7 application that consumes REST services. REST services lead to less overhead and standard HTTP operations. Also, these services help in easy and quick development.


Similar Articles