Welcome!

Microsoft Cloud Authors: Kevin Benedict, Pat Romanski, Liz McMillan, Lori MacVittie, Elizabeth White

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

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 .

More Stories By Derek Ferguson

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

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
SYS-CON Events announced today that Silicon India has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Published in Silicon Valley, Silicon India magazine is the premiere platform for CIOs to discuss their innovative enterprise solutions and allows IT vendors to learn about new solutions that can help grow their business.
We are seeing a major migration of enterprises applications to the cloud. As cloud and business use of real time applications accelerate, legacy networks are no longer able to architecturally support cloud adoption and deliver the performance and security required by highly distributed enterprises. These outdated solutions have become more costly and complicated to implement, install, manage, and maintain.SD-WAN offers unlimited capabilities for accessing the benefits of the cloud and Internet. ...
Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT staff augmentation services for software technology providers. By providing clients with unparalleled niche technology expertise and industry experience, Chetu has become the premiere long-term, back-end software development partner for start-ups, SMBs, and Fortune 500 companies. Chetu is headquartered in Plantation, Florida, with thirteen offices throughout the U.S. and abroad.
SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
Business professionals no longer wonder if they'll migrate to the cloud; it's now a matter of when. The cloud environment has proved to be a major force in transitioning to an agile business model that enables quick decisions and fast implementation that solidify customer relationships. And when the cloud is combined with the power of cognitive computing, it drives innovation and transformation that achieves astounding competitive advantage.
DXWorldEXPO LLC announced today that "IoT Now" was named media sponsor of CloudEXPO | DXWorldEXPO 2018 New York, which will take place on November 11-13, 2018 in New York City, NY. IoT Now explores the evolving opportunities and challenges facing CSPs, and it passes on some lessons learned from those who have taken the first steps in next-gen IoT services.
Cloud-enabled transformation has evolved from cost saving measure to business innovation strategy -- one that combines the cloud with cognitive capabilities to drive market disruption. Learn how you can achieve the insight and agility you need to gain a competitive advantage. Industry-acclaimed CTO and cloud expert, Shankar Kalyana presents. Only the most exceptional IBMers are appointed with the rare distinction of IBM Fellow, the highest technical honor in the company. Shankar has also receive...
DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by FinTechEXPO. ICOHOLDER gives detailed information and help the community to invest in the trusty projects. Miami Blockchain Event by FinTechEXPO has opened its Call for Papers. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Miami Blockchain Event by FinTechEXPOalso offers sp...
DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
@DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time t...