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.


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


3. Select Web Service, then click Add.


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.


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


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


Adding Web Service Reference

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


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.


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.


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.


Next post:

Previous post: