In this article I would like to show you how we can consume the web services in Microsoft .Net compact framework. For that, first I will give brief introduction about compact framework and then I will create a simple web service, then will show how we can consume that web service in Compact framework application.
What is Compact Framework?
The Microsoft .NET Compact Framework is a subset of the .NET Framework that is designed to run on resource-constrained devices, providing support for managed code and for Extensible Markup Language (XML) Web services. The .NET Compact Framework is available for devices running the Microsoft Windows CE operating system, such as Pocket PC and Pocket PC 2002/2003 devices, and devices running Windows CE .NET. OK, definitions are good, so what we need to develop Compact Frame work applications? The answer is simple, Visual Studio.NET. Though we can develop Compact Frame work applications in Visual Studio.NET, right now it is available in VB.NET and C# languages, in future enhancements, they might add other languages also into it. Also right now all the Pocket PC devices are not supported to run .Net CF. Please see http://www.microsoft.com/windowsmobile/products/pocketpc/default.mspx the URL for more info on it.
Why we need to integrate Compact framework with Web Services?
If you see the trend of business these days they are looking to grow their business by using a word "Globalization". To satisfying the business needs even IT also started designing/architecting the applications in a new dimension by using XML in their applications. Even in .Net, XML has it's own importance. If you talk about XML, then next thing is web services! Yes, this web services technology is the one satisfying the business need of word "Globalization". So as a next step, this .NET Compact Framework that is used to enable .NET applications on Pocket PC supports Web services natively.
What else we have to know about Compact Framework:
You will need Visual Studio .NET 2003 to build applications that target the .NET Compact Framework. As I mentioned earlier you can build applications using either Visual C# .NET, Visual Basic .NET, or both. The .NET Compact Framework has two main components a) The common language runtime b) The .NET Compact Framework class library. The runtime is the foundation of the .NET Compact Framework. It is responsible for managing code at execution time, providing core services such as memory management and thread management while enforcing code safety and accuracy. The .NET Compact Framework class library is a collection of reusable classes that you can use to quickly and easily develop applications.
Since it is a Compact framework, some of the features are removed which we used to get from .Net framework. For example you don' have ToolTip control, if you look into Compact framework, most of the method overloads are eliminated. For XML developer, Xpath is removed from System.XML name space. One last important thing for Web Service integrators is; Cookies are eliminated for now in Compact Framework.
Moving to Reality:
OK, we learned a bit about Compact framework. Now we will see how we can consume web service in Compact framework. For that first we will create a simple web service, which takes a string and reverses that string and returns back that string to us. If you are a beginner in developing web services, please take help of Beginning Web Service Development article at http://www.w3coder.com/ws/WSGWBook/WebServGW-02.xml. Please use below code to develop that web service and name it as service.asmx.
<WebMethod()> Public Function RevString(ByVal ReverseString As String) As String
Dim newstring As String
Dim i As Integer
newstring = ""
For i = Len(ReverseString) To 1 Step -1
newstring = newstring & Mid$(ReverseString, i, 1)
RevString = newstring
Please test the web service. Then our next step is of consuming the web service into Compact Framework.
Creating a Compact Framework Application:
For that first open visual studio.net and Start a new project. Then From the next screen, select "Visual Basic Projects" from the project types and then select "Smart Device Application" From the Templates. Then Type Name and Location in the related text boxes and press OK to create a project. Now you are going to see "Smart Device Application Wizard". In that you are going to see two list boxes, first list box is going to ask you "What platform do you want to target?" in that select "Pocket PC", second list box is going to ask you "What Project Type do you want to create?" from that select "Windows Application" and press "OK".
The selected option project types can be used to create applications that target Pocket PC devices running SH3, MIPS or ARM processors and Pocket PC 2002/2003 devices with ARM or XScale processors.
The Windows Application project including Pocket PC specific controls. Right now in our project we selected this option. This is the most commonly used project type.
The Class library Project allows us in creating package related components in a single file. These can be used to develop other applications or as a base for inheritance for other components.
Used to create executables that will not have a user interface. Best used for background and maintenance applications that do not require user interaction.
Defines a project with no predefined components
Table 1.1 Pocket PC project types
It creates a project with Form1.vb as default form. Now our target is of consuming the web service created above. Before consuming it, let’s keep a Textbox and a Button controls in the form (drag and drop the Textbox and Button controls from Toolbox to form1).
Now let’s consume the web service. As we will do in windows form application here also for consume web service, go to references (In Solution Explorer), right click it, in that select "Add Web references". In that type the URL of the web service and press "Add Reference" Button to add web service to your project.
If you are adding the web service from your local machine, try to give your machine name in place of local host. In other words, while adding web service, instead of typing http://localhost/ws/service.asmx, type http://YourMachinename/ws/service.asmx (other wise, application won’t recognize WS at runtime)
OK, we have a web service reference into our project. Our next step is of using in our application, for that in Button Click event, add below code.
Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
Button1.Text = "Processing!!"
Dim ws As New w3coder.Service
ws.Url = "http://machinename/service.asmx"
TextBox1.Text = ws.RevString(TextBox1.Text)
Cursor.Current = System.Windows.Forms.Cursors.Default
Button1.Text = "Reverse String"
Code 1.1a for consuming webservice in button click event.
If you see the above code, it's very simple, all what I am doing is, before calling the web service, I am changing the cursor as wait, to show that it is processing. Then by using "Dim ws As New w3coder.Service" code I am instantiating the web service, in that step you have to add your web service instance code. Next by using of ws.URL, I am reassigning URL location. This line is added to show you how we can dynamically assign web service location. If you are reassigning again, then please remember to use machine name instead of localhost. Then, by using of ws.revstring we are calling the function from the web service. Once the process is over, we are again changing the cursor to default mode. That's it, simple code! Now our next step is of building the application.
To build the application, go to tool bar select "Build" option, in that select "Build solution" or "Build PRJname" (In place of PRJname, you will see your project name). Once you are done that, you should see below 1.1bmessage.
------ Build started: Project: apptwo, Configuration: Debug Pocket PC ------
Performing main compilation...
Building satellite assemblies...
Visual Studio is ready to deploy apptwo
---------------------- Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
Message 1.1b Build Project message in output screen
In the place of apptwo you will see your project name in 1.1b message. If you see 0 failed, then you are good to go! Now our next step is of testing in Pocket PC. Here we will test in Pocket PC Simulator, which we got with Visual Studio.NET. To do it, either run application or from the Build menu, select "Deploy PRJName" (In place of PRJname, you will see your project name). Then it's going to popup a message box, of asking you to select a Pocket PC type as shown in figure 1.2
Figure 1.2 Deploy options
From there, select "Pocket PC 2003 Emulator", and press "Deploy" Button. Also if you don't want to see this screen from next time onwards, Uncheck “Show me this dialog each time I deploy the application” Check box.
It will open emulator, first time it will deploy .Net compact framework's needed software and then it will installs the EXE, which we created. First time it will take little bit longer time to install framework and exe. Figure 1.3 shows the installation process in Emulator.
Figure 1.3 Installation Process in Emulator.
While deployment process you will see similar to message 1.1C in output screen.
------ Deploy started: Project: apptwo, Configuration: Debug Pocket PC ------
Deploying to Pocket PC 2003 Emulator using Emulation Transport
Connected to Pocket PC 2003 Emulator (Pocket PC) running on WCE420X86.
Copying files from 'd:\nettest\apptwo\bin\Debug' to '\Program Files\apptwo'
Launching device installation of 'System_SR_enu.cab'. Please check device screen for further instructions..
---------------------- Done ----------------------
Build: 1 succeeded, 0 failed, 0 skipped
Deploy: 1 succeeded, 0 failed, 0 skipped
Message 1.1c After Deployment process in pocket PC Emulator message in output window
Again here also if you see 0 failed message, then you are good to go. If you press Deploy button in previous step for running the application, then explicitly you have to browser for your EXE, in the emulator, for that you can go to STARTà Programs, in that use file explorer and search for executable and click on it to start (You can get the location from Deploy output message 1.1C) or else it will directly runs the exe for you. Next step is run the application. For that enter some text in text box and press button. It should process and show the reverse of the string in same text box. Sample picture is shown in figure 1.5.
Figure 1.5 Exe in Pocket PC Emulator.
Consuming Web Services is as easy as we do in ASP.NET or windows applications in Compact framework also. The important thing is, you have to keep Compact Framework limitations in your mind while developing or consuming Web Services.
Source code: DownloadZip
About The Author:
Sreedhar Koganti is a Microsoft Certified Professional from Falls Church, VA. He has several years of experience in developing and implementing applications for Microsoft platforms as a consultant and author. He also teaches Microsoft technologies and has a free educational site (http://www.w3coder.com) that contains .NET-related content. Recently he contributed to ASP.NET Developer's Cookbook (see /cookbook).