This article will demonstrate how to call .NET web service from Pocket PC application. The Pocket PC application will pass data using Pocket SOAP (Simple Object Access Protocol) to the web service (therefore IIS with .NET framework is required on the server) and return result back to the Pocket PC application.
What will you need?
- Microsoft eMbedded Visual Tools (which is free by the way).
- Pocket SOAP from http://www.pocketsoap.com/ .
- This article assumes that you already know how to create a web service.
- Web Server with Microsoft .NET framework installed.
- Internet connection from Pocket PC to the web-server running the web service.
Microsoft eMbedded Visual Tools:
The Microsoft® eMbedded Visual Tools 3.0 deliver a complete desktop development environment for creating applications and system components for Windows® Powereddevices, including the Pocket PC and Handheld PC.
For more information and how to download Microsoft® eMbedded Visual Tools, please click on following link http://www.microsoft.com/mobile/downloads/emvt30.asp
This is a SOAP client COM component for the Windows family, originally targeted at PocketPC (hence the name), there is also a Win32 version that works on Windows 95/98/Me/NT4/2000/XP. The Pocket SOAP implementation is nicely packaged into a COM component running on a Pocket PC. The component is written in Microsoft eMbedded Visual C++® (eVC), and the source is available for download, too (you will find license information on the site). There is also a version for PCs (Microsoft Windows® 95/98/ME/2000 and Microsoft Windows NT® 4).
For more information and how to download Pocket SOAP, please click on following link: http://www.pocketsoap.com/pocketsoap/
Cross Platform and XML Web Services
If you are a developer and have been living on a remote Island from past two years with no internet connection and no Microsoft's sales rep around you then you missed the big news, Microsoft® finally did it, "Cross platform development" – wait, that's very strong statement, so let me rephrase "Cross platform development using .NET Web Services" and bad news is that you will need to read up on SOAP.
Web services changed the way developers design and develop applications nowadays. Now not only the functionality can be shared with-in the application but also across different platforms and over the internet. This gives new meaning to application development for wireless devices including Pocket PC. In essence, using SOAP developers can write an application for Pocket PC that is not only very rich in functionality but also requires limited amount of memory.
How to create a Web Services:
As this article is more gear towards calling a web service from Pocket PC, I don't want to spend a lot of time on how to create a web service but on how to call a web service from eMbedded Visual Basic. If you are new to web services then take a look at following article: Create Web Services and Web Forms Programmatically with the .NET Framework SDK.
Code walk through:
Following is a code which implements Mortgage web service. The web service has three input params (Number of months, Interest Rate and Loan Amount) and it returns monthly mortgage payment.
Option Explicit On
Public Class MortgageWebService
<WebMethod(), SoapRpcMethod()> _
Public Function MonthlyPayment( _
ByVal nMonths As Short, ByVal InterestRate As Double, _
ByVal loanAmt As Decimal) As Decimal
Dim monthlyRate, tempVal As Double
Dim unRoundedAmt As Decimal
monthlyRate = InterestRate / 1200
tempVal = Math.Pow(1 + monthlyRate, CDbl(nMonths))
unRoundedAmt = CDec(CDbl(loanAmt) _
* (monthlyRate*tempVal / (tempVal - 1)))
Return Decimal.Round(unRoundedAmt, 2)
Following is how the web service looks like in a web browser:
Just to be safe, try to call the web service from Pocket IE using emulator that comes with EVT.
Now comes the fun stuff, how will we call this web service from Pocket PC? Pocket SOAP will do that for you and it even gets better, there is wizard available at http://www.pocksoap.com/ which will write a code for you. But we will do it old way here.
Following is how the application looks like Pocket Emulator:
Code behind the application:
Private pEnvelope As PocketSOAP.CoEnvelope
Private pHTTP As PocketSOAP.HTTPTransport
Private Sub Calculate_Click()
Set pEnvelope = CreateObject("pocketSOAP.Envelope.2")
pEnvelope.MethodName = "MonthlyPayment" ' Method Name
pEnvelope.URI = "http://localhost/" ' Name Space
pEnvelope.Parameters.Create "nMonths", numMonths.Text
pEnvelope.Parameters.Create "InterestRate", InterestRate.Text
pEnvelope.Parameters.Create "loanAmt", loanAmt.Text
Set pHTTP = CreateObject("PocketSOAP.HTTPTransport.2")
' Soap action path, you can find that in WSDL file
pHTTP.SOAPAction = "http://localhost/MonthlyPayment"
' Path of actual web service
pHTTP.Send "http://localhost/MortgageSample/Service1.asmx", _
Label1.Caption = "$" & pEnvelope.Parameters.Item(0).Value
Some comments on the code:
- You can find MethodName, URI, and SOAPAction information in WSDL file: e.g. http://localhost/MortgageSample/Service1.asmx?WSDL
- As Web Services Description Language (WSDL) defines two styles for how an XML Web service method, which it calls an operation, can be formatted in a SOAP message: RPC and Document. RPC formatting refers to formatting the operation according to the SOAP specification for using SOAP for RPC; otherwise known as Section 7 of the SOAP specification. RPC formatting states that all parameters are encapsulated within a single XML element named after the XML Web service method and that each XML element within that XML element represents a parameter named after the parameter it is representing.
- If your web service is returning Array then you can write a custom serializer to extract the data into a more useable form. The new WSDL tools will do all this for you, there's a alpha version available at www.pocketsoap.com/pocketsoap/beta/wsdl.asp
- If you are using Pocket Emulator to develop, make sure you download binaries files fro PocketSOAP emulator from www.pocketsoap.com
This is very simple example to show how web services can be used for development of great application. Now it's up to you to create great applications e.g. Microsoft just release MapPoint .NET Basic Services 2.0 and you can take advantage of the mapping and spatial analysis functionality of this new XML Web Service. I hope you will take advantage of web services for the Pocket PC development, as it gives developers freedom to develop in any language and leveraging true power of OO development.
Also Microsoft Pocket PC 2002 SDK is been released, I haven't used it yet but I am sure it does have new .NET features. I hope you are excited about this as much as I am – and you will hit the ground running.