Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Computing
Conference & Expo
November 2-4, 2009 NYC
Register Today and SAVE !..
SYS-CON.TV
Today's Top SOA Links


Invoking .NET Web Services from Mobile Devices Part 1 of 2 Pocket PCs put .NET Web services at your fingertips
Invoking .NET Web Services from Mobile Devices Part 1 of 2 Pocket PCs put .NET Web services at your fingertips

In May 2000 I was invited to Microsoft's corporate headquarters in Redmond for a special "technology experts" summit. At this summit, the forty or so of us in attendance were given a special sneak preview of a technology upon which Microsoft planned to "bet the farm," so to speak. They called the technology "ASP+ Web Methods."

Of course, nowadays we all know these as .NET Web services. The fundamental technologies upon which they are based (XML, SOAP, SDL, UDDI, DISCO, etc.) are probably familiar to just about all of this publication's readership in one form or another.

What many Web service developers are not aware of, however, is how good .NET is at "playing nice" with all sorts of different mobile devices. The Mobile Internet Toolkit, for example, adds out-of-the-box support for WAP and i-Mode clients to .NET's already formidable Web application infrastructure. Similarly, the Smart Device Extensions for .NET and the .NET Compact Frameworks bring the power of the .NET Common Language Runtime (CLR) environment to a wide range of wireless Internet devices, such as the Pocket PC.

The focus of this magazine, however, is on Web services. For this reason, in this article I'll show you how .NET Web services may be accessed and utilized from many different devices.

The code excerpts in this article have all been taken from my book, Mobile .NET (Apress, 2001). If you find this article interesting, I recommend that you purchase a copy!

Creating the .NET Web Service
When it comes to teaching people Web services, stock-quoting applications seem to have taken over the role that "Hello World" served so elegantly with older technologies. This is to say that a Web service capable of returning information - or simulated information - about the current price(s) of various stocks seems to be the example of choice in better than 90% of all Web service literature out there.

So, for our example mobile Web service, we'll develop a Web service that takes a single, four-letter stock symbol as its sole parameter and returns a single string to indicate the current price of this stock. To fuel the Web service with up-to-the-minute data, we'll make use of Lycos' excellent Finance Web site.

To create the sample Web service, follow these steps:

  1. Open the Windows Start menu.
  2. Locate the Microsoft Visual Studio.NET 7.0 Program Group.
  3. Launch Microsoft Visual Studio.NET 7.0.
  4. Click "New Project" on the start page.
  5. Select a Project Type of "Visual Basic Projects".
  6. For a template, choose "ASP.NET Web Service".
  7. Name your Web service "Chapter10Serv er" and click "OK".
  8. In the Solution Explorer, right-click "Service1.asmx".
  9. Choose "View Code" from the pop-up context menu.
  10. Enter the code shown in Listing 1.
What does it all mean?
Now that you've entered all of the code for your Web service, it makes sense to pause for a moment to review exactly what it all means. The first two "imports" lines in Listing 1 tell .NET to include support for Web services within our Visual Basic application. Specifically, the first line tells .NET to include general support for Web services. The second line tells .NET to include support for Web service protocols other than .NET's native Web services protocol.

This second line is very important. Looking at the industry as a whole, simple SOAP is still the most common protocol used for invoking Web services. Many of the toolkits out there for building Web services clients do not yet support Microsoft's full Web services protocol. For this reason, it's easier to make .NET "speak their language" than vice versa!

After these first two lines, we have a couple of lines that ask .NET to include support for low-level networking (e.g., TCP/IP socket communications) and regular expression parsing. Regular expressions are a new addition to Visual Basic with .NET, and will be greatly welcomed by those with previous experience in other languages such as Perl.

We make our class into a Web service under .NET by inheriting it from System.Web. Services.WebService. The tag preceding our class declaration is .NET nomenclature for declaring a namespace for our Web service.

A similar tag is prepended to our getQuote method's declaration to indicate that this method should, indeed, be exposed to the Internet as a Web service. However, within this tag there is also the indication "SoapRpc Method()." This is how we leverage .NET's support for industry-standard Web services protocols to expose our method as a standard remote SOAP method.

Our getQuote method takes a single parameter, the symbol of the stock for which we are interested, in receiving a quote. The first thing our method does is use .NET's regular expressions parsing capabilities to verify that the symbol consists of exactly four letters. If this isn't the case, an exception is thrown and the Web service call terminates.

Assuming that the symbol is properly-formed, however, the next thing that our method does is to pass this symbol along to Lycos' Quote.com Web site to get a price quote. This is accomplished by using .NET's TCP/IP capabilities to send a simple Web request across the Internet. The contents of the page returned are stored as a (very long) string is the strResponse variable.

At this point, we once again leverage .NET's regular expressions to parse the Web page for the first occurrence of the phrase "Last Sale" that is followed by numbers, a decimal, and then more numbers. This is the general area of the page where our most recent stock price has been returned.

From within this section, the numerical portion is finally parsed out. This is the value that is returned by our method as the results of any Web service invocation.

Creating the Pocket PC Client
In many ways, Pocket PCs are probably Microsoft's favorite mobile devices. For this reason, we'll begin by showing you how to invoke NET Web services from these devices. To follow along with this discussion, you'll need:

  1. Microsoft eMbedded Visual Basic, and
  2. Either a Pocket PC, or a Pocket PC emulator
  3. Pocket SOAP
eMbedded Visual Basic and a Pocket PC emulator can both be downloaded for free as part of Microsoft's eMbedded Visual Tools. The URL to this download site is: www.microsoft.com/mobile/downloads/emvt30.asp.

The package will come to you as a standard Windows installer. Simply walk through the installation wizard and make sure that you choose to install eMbedded Visual Basic and (if you don't have a "real" Pocket PC) the Pocket PC Software Development Kit (SDK).

Simon Fell's Pocket SOAP software is currently the most popular tool for invoking SOAP-style Web services from Pocket PCs. To obtain this and install it onto your device or emulator, follow these steps:

  1. Browse to www.pocketsoap.com.
  2. Follow the links to version 1.1 of the software.
  3. If you have a "real" Pocket PC, download the Pocket PC install. If you have the Pocket PC emulator, download the x86 binaries.
The Pocket PC Install
Putting the Pocket PC install onto an actual Pocket PC device is a fairly easy process. First, make sure that your PC is connected to your computer and that ActiveSync is functioning properly. Then, run the Pocket PC install. It will walk you through an installation wizard, then ActiveSync will finish the installation of the software onto your device.

Installing the x86 binaries onto the emulator is a little more complicated. You must:

  1. Unzip the archive that you downloaded.
  2. Open the Windows Start menu.
  3. Locate the Microsoft Windows Platform SDK for Pocket PC program group.
  4. Start the "Emulation Environment for Pocket PC" within this group.
  5. You are now presented with a command prompt at a certain location.
  6. Copy the "pSOAP.dll" from the archive in step #1 above to the location in step #5 above.
  7. Within the Emulation Environment, type "regsvrce".
  8. Once the Pocket PC emulator starts, type "\windows\pSOAP.dll" where asked for the full pathname, then click "OK".
You are now (finally) ready to create the code for the Pocket PC Web services client. Simply choose to create a new Pocket PC project under eMbedded Visual Basic, add a single text box and command button to the project's default form, and then add the code from Listing 2 to the command button's Click event.

The first several lines of code in Listing 2 are concerned with creating two of the most important objects in Pocket SOAP: the Envelope and the Transport. A Pocket SOAP Envelope is where the body of the SOAP message is assembled.

The Transport is responsible for making sure that the message created by the Envelope gets from the device to the Web service, and for receiving the Web service's response. Several kinds of Transport are possible within the framework of SOAP: SMTP, FTP, etc. For our purposes, we've chosen to use the HTTP Transport, which will convey our messages using standard Web traffic.

The next three lines of Listing 2 set various properties for our SOAP request. Specifically, the URI must be set equal to our Web service's namespace. The MethodName should be the name of the method within our Web service that we wish to invoke. Finally, you should call CreateParameter with the name and value of every parameter that you must pass in.

The next thing we do is call the Serialize method on our Envelope object to translate our message into a simple string. We store this string in a variable and then call MsgBox to display it for your inspection. This is strictly for demonstration and debugging purposes. In a true, production Mobile .NET application, this step would typically not be taken.

We call the Send method on our HTTP Transport object and pass in the URL of our Web service and our Envelope as parameters. This is how our SOAP request travels from the Pocket PC to the remote Web service.

We then call the Receive method on our HTTPTransport object - this call blocks until our Web service responds. The response is returned as a string which we store in a variable named strResponse.

By passing this string into our Envelope object's parse method, we're able to translate the text message that our Web service sent back into a real SOAP response. The code concludes by extracting the price of our stock out of this response and displaying it in a message box for the user.

In Conclusion
As you've seen in this article, creating a .NET Web service that can be accessed and utilized from a mobile Internet device is easy! Thankfully, Simon Fell has provided an excellent toolkit in his Pocket SOAP software for invoking .NET Web services from Pocket PCs.

Recently, however, Microsoft publicly released a brand new technology that promises to make calling .NET Web services from Pocket PCs even easier. This technology is known as the .NET Compact Framework and will be discussed at length in the next installment of this two-part series.

In the meantime, if you have any questions or comments, please contact me via my Web site at www.MobileDotNet.com .

About Derek Ferguson
Derek Ferguson, founding editor and editor-in-chief of .Net Developer's Journal, is a noted technology expert and former Microsoft MVP.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021

SYS-CON Featured Whitepapers
ADS BY GOOGLE