Welcome!

Microsoft Cloud Authors: Janakiram MSV, Yeshim Deniz, David H Deans, Andreas Grabner, Stackify Blog

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.


@ThingsExpo Stories
SYS-CON Events announced today that SIGMA Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. uLaser flow inspection device from the Japanese top share to Global Standard! Then, make the best use of data to flip to next page. For more information, visit http://www.sigma-k.co.jp/en/.
SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
SYS-CON Events announced today that NetApp has been named “Bronze 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. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp em...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that B2Cloud will exhibit at 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. B2Cloud specializes in IoT devices for preventive and predictive maintenance in any kind of equipment retrieving data like Energy consumption, working time, temperature, humidity, pressure, etc.
DevOps at Cloud Expo – being held October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA – announces that its Call for Papers is open. Born out of proven success in agile development, cloud computing, and process automation, DevOps is a macro trend you cannot afford to miss. From showcase success stories from early adopters and web-scale businesses, DevOps is expanding to organizations of all sizes, including the world's largest enterprises – and delivering real r...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" 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. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Suzuki Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Suzuki Inc. is a semiconductor-related business, including sales of consuming parts, parts repair, and maintenance for semiconductor manufacturing machines, etc. It is also a health care business providing experimental research for...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
SYS-CON Events announced today that Keisoku Research Consultant Co. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Keisoku Research Consultant, Co. offers research and consulting in a wide range of civil engineering-related fields from information construction to preservation of cultural properties. For more information, vi...
SYS-CON Events announced today that Mobile Create USA will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Mobile Create USA Inc. is an MVNO-based business model that uses portable communication devices and cellular-based infrastructure in the development, sales, operation and mobile communications systems incorporating GPS capabi...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Daiya Industry specializes in orthotic support systems and assistive devices with pneumatic artificial muscles in order to contribute to an extended healthy life expectancy. For more information, please visit https://www.daiyak...
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
Elon Musk is among the notable industry figures who worries about the power of AI to destroy rather than help society. Mark Zuckerberg, on the other hand, embraces all that is going on. AI is most powerful when deployed across the vast networks being built for Internets of Things in the manufacturing, transportation and logistics, retail, healthcare, government and other sectors. Is AI transforming IoT for the good or the bad? Do we need to worry about its potential destructive power? Or will we...
SYS-CON Events announced today that mruby Forum will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. mruby is the lightweight implementation of the Ruby language. We introduce mruby and the mruby IoT framework that enhances development productivity. For more information, visit http://forum.mruby.org/.
SYS-CON Events announced today that Nihon Micron will exhibit at the Japan External Trade Organization (JETRO) Pavilion at 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. Nihon Micron Co., Ltd. strives for technological innovation to establish high-density, high-precision processing technology for providing printed circuit board and metal mount RFID tags used for communication devices. For more inf...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.