Web Service Consumption (Silverlight For Windows Phone) Part 1

Web service has become a standard when it comes to using a predefined web function in our application. In this section we will learn how to consume web service on Windows Phone. Web services consumable by Windows Phone can be in the form of SOAP (built in WCF or other technologies), plain HTTP, or REST. Explanations regarding the said terms will not be discussed here; if you are not familiar with the terms you can look it up in your preferred search engine.

Windows Phone applications can access these web services either directly or through a proxy class that is automatically generated from metadata attached to a service. A service can be in a form of user defined service that you place in your server, or a third party server, for example Facebook, Twitter, and other services. Silverlight can work with many different data format, such as XML, JSON, RSS, or ATOM, and data access can be done under numerous scenarios, such as serialization, LINQ to XML, LINQ to JSON, or syndication. It has unlimited combinations, and can be implemented based on your need.

Access via Generated Class

Accessing a service through a proxy class generated from metadata can improve development speed. This section will discuss an example of web service access using .NET technology with auto-generated proxy class.

Creating Web Services

1. Run a Visual Studio program that support Web Application project creation (in this case I use Visual Studio 2010 Web Developer Express), then create a new Web ASP.NET Web Application project.


tmp1C-59

2. On the Solution Explorer window, add a web service file by right clicking on Project, then select Add > Add New Item.

tmp1C-60

3. Select Web Service, then click Add.

tmp1C-61

4. We will only use the automatically generated function, which is a web service method that returns a "Hello World" string. Further explanations regarding web service will not be discussed, and we will focus to the Windows Phone access aspect.

tmp1C-62

5. Right click on the web service file and select View in Browser to test the function.

tmp1C-63

6. Your sample web service is ready for consumption. Do not turn off your browser.

tmp1C-64

Adding Web Service Reference

1. Open your Windows Phone project. On the Solution Explorer windows, right click on References, then select Add Service Reference.

tmp1C-65_thumb

2. Copy and paste the access address for the web service that we created. This address can be retrieved from the URL address on the browser. Click Go.

tmp1C-66

3. If the web service is found and accessible, a list of services and available operations will be displayed. Give a namespace per your need then click OK.

tmp1C-67

4. If the addition is successful, on the Solution Explorer window we can see a configuration file and a reference file for the web service we created.

tmp1C-68

Next post:

Previous post: