perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home »» Info Bank »» Articles » XML Web Services and Mobile Web Clients Tuesday, 18 September 2007
 

Back to Articles Page      

        

XML Web Services and Mobile Web Clients

Andy Wigley XML and WebServices 2002
About the author:
Andy Wigley is the co-author of the book Building .NET Applications for Mobile Devices (Microsoft Press, 2002, ISBN: 0-7356-1352-2). He will be speaking at XML & WebServices 2002, which will be taking place on 11-13 March at Queen Elizabeth II Conference Centre, London.

Click here to see the show preview details.

Authors Note: The following extract is from the book Building .NET Applications for Mobile Devices by Andy Wigley and Peter Roxburgh (Microsoft Press, 2002, ISBN: 0-7356-1352-2). This book explains how to use the Microsoft Mobile Internet Toolkit to create ASP.NET applications that you can use with mobile devices such as Pocket PCs, Palm PDAs, WAP and iMode mobile phones. To learn more about the book or to purchase it, go to http://www.microsoft.com/mspress/books/5624.asp.

XML Web Services offer big advantages to the mobile Web developer. They provide data over the Web that is not formatted with any particular browser in mind. Using Microsoft Visual Studio .NET, it is an easy matter to consume an XML Web service in a Mobile Internet Toolkit application and to access the data from a wide variety of mobile devices, as the following extract demonstrates.

XML Web services offer an exciting, new way to provide programmable components remotely. They allow you to access services that you, Microsoft, or a third party provide. If you're a traditional Web developer, you'll need to undergo a paradigm shift in order to really understand XML Web services. If you're a programmer who uses existing forms of remote procedure calls, you're in for a pleasant surprise.

XML Web services differ immensely from previous proprietary solutions. For one thing, they're platform independent and language independent; programs written in any language (not just the languages supported by .NET) and running on any platform can consume them. For another, they're based on existing, open protocols such as HTTP, SOAP, and XML. Moreover, unlike other remote access methods, XML Web services can navigate today's Internet firewall landscape. Using XML as a means to communicate data between a client and an XML Web service further enhances this powerful component architecture.

You might think that XML Web services simply provide remote procedure calls that are particularly well suited for Internet deployment because you can consume these services regardless of the client’s language. Although this is true, unlike many earlier component technologies, XML Web services offer much more. When you make a call to an XML Web service, the server returns a response in XML. Using .NET Framework tools or Visual Studio .NET, you can add automatically generated code to your ASP.NET project that handles communication with the Web service and transparently parses the XML so that you can write code as though that XML Web service’s methods were local. In other scenarios, such as Common Gateway Interface (CGI) development using Perl, you have to parse the XML that the XML Web service returns before you can use the returned values. Alternatively, you can directly access a service from a Web browser that supports XML, such as later versions of Microsoft Internet Explorer. In fact, because XML Web services can communicate over HTTP, you can even make a call to an XML Web service from a Telnet session!

Creating an XML Web Service

This first example will show you just how easy it is to create an XML Web service. In keeping with tradition, you'll write a "Hello World!" XML Web service.

Using a Text Editor

You store XML Web services in files that have an .asmx extension in a virtual directory on your Web server, just like standard mobile Web pages. Create a virtual directory, and call it MyFirstWebService. Using a text editor, create a new file named MyWebService.asmx, containing the following code:
<%@ WebService Language="c#" Class="MyWebService" %>

using System;
using System.Web.Services;

[WebService(Namespace="http://127.0.0.1/MyFirstWebService/")]

class MyWebService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}
Listing 1: MyWebService.asmx

Although this code resembles a typical ASP.NET "Hello World!" program, it has a few important differences:
  • The first line declares that the code is an XML Web service. You must include this declaration in all XML Web services.
  • You import System.Web.Services, which your class will extend.
  • You make a namespace declaration after the using statements. Unlike other mobile Web Forms code-behind modules, you must include a namespace declaration in an XML Web service. You declare the namespace using a full URL.
  • The class extends the System.Web.Services.WebService class. You can inherit some useful methods and properties of this class; however, you don't have to inherit from this class to create an XML Web service.
  • The [WebMethod] declaration that precedes the HelloWorld method signifies that the HelloWorld method is accessible as an XML Web service. To make the method available as an XML Web service, you must declare it public.
That's it! You've completed your first XML Web service.

Deploying and Consuming an XML Web Service

The simplest way to deploy an XML Web service is to leave the .asmx file in the virtual directory on your Web server. You deploy XML Web services that you build using Visual Studio .NET in exactly the same way as an ASP.NET application, either by using the Copy Project facility or by building a Visual Studio .NET Setup and Deployment project.

You can call this XML Web service from a Web browser and access its Web methods, as Figure 1 shows. However, in real life, you'll probably add code to an application you're writing to access the XML Web service. This section describes how to consume an XML Web service programmatically.
Directly calling an XML Web service from a Web browser
Figure 1 Directly calling an XML Web service from a Web browser

Using Visual Studio .NET

Create a new mobile Web application, and call it ConsumeMyFirstWebService. This application will contain the mobile Web Forms page that will consume the XML Web service you created earlier. Drag a single Label control onto the mobile Web Forms page. The Label control should have a blank Text property.

The process of consuming an XML Web service has two stages. The first stage entails adding a Web reference to the XML Web service you want to consume:
  1. In the Visual Studio .NET menu, Click Project.
  2. Select Add Web Reference from the menu.
  3. When the Add Web Reference dialog box appears, click the Web Reference On Local Web Server link. Figure 2 depicts this dialog box.
  4. Wait a few seconds for both sides of the dialog box to display information. The left-hand side of the dialog box displays the contents of the server’s discovery file. The right-hand side shows a list of available XML Web services. Select the XML Web service you want to consume.
  5. The display changes to show two links: View Contract and View Description. The View Contract link displays the service description that you learned about earlier. The View Description link displays a description of the XML Web service. Select either link to see the document it references. Otherwise, click the Add Reference button at the bottom of the dialog box.

Add Web Reference dialog
Figure 2 Add Web Reference dialog


You've now added a Web reference to the XML Web service you want to consume. The runtime has automatically created the proxy class you use to access the XML Web service, and this proxy class is now part of your application. To use the methods of this class, you must first import the namespace it occupies—the main namespace of your application. Here's the syntax:

using ConsumeMyFirstWebService.localhost;

Now you can use any of the methods of the XML Web service as though they resided on your local machine. In this example, you can access the HelloWorld method of the XML Web service. You can use the result to set the value of the Label control's Text property in the mobile Web Forms page you created earlier by adding this syntax to the code-behind module:

private void Form1_Activate(Object sender, System.EventArgs e)
{
    // Create a new instance of the Web Service class.
    Service1 service1 = new Service1();

    // Call the HelloWorld method.
    String msg = service1.HelloWorld();

    // Assign the result to the Text property of the Label.
    Label1.Text = "From Web Service: " + msg;
}
Note If you're unsure of a class or method name, you can find it by viewing the WSDL file for the XML Web service. To do so, you either access the service description by using a browser (as described previously), or you view the WSDL document within Visual Studio .NET. You can access the document through Solution Explorer. If you can't see a file with a .wsdl extension, click the Show All Files icon to ensure that all the project's files appear.
When you execute the code, the Form1_Activate method creates a new instance of the Service1 class, which is the proxy object. You then can call the HelloWorld method on this object. Next the proxy object makes a request to the remote XML Web service, which returns an XML response. The runtime parses the response and assigns the string msg the value of the response. Figure 3 shows the output of this code, as viewed on a Pocket PC and the Openwave™ WAP emulator.
Output of the Mobile Internet Toolkit on Pocket Internet Explorer and on the Openwave™ WAP browser

Figure 3 Output of the Mobile Internet Toolkit on Pocket Internet Explorer and on the Openwave™ WAP browser
Image of UP.SDK courtesy Openwave Systems Inc. Openwave, the Openwave logo, and UP.SDK are trademarks of Openwave Systems Inc. All rights reserved.



  

Back to Articles Page      

  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy