he mobile WAP client is the first Web services client I cover in this series. I selected it to give you insight into the Microsoft Mobile Internet Toolkit (MIT), which helps developers create mobile applications. Currently in Beta 2, this toolkit easily plugs into Visual Studio.NET Beta 2.
I like the MIT because it provides a visual interface for tedious mobile programming. Although Wireless Markup Language (WML) itself is very simple, it is unfortunately difficult to build complex solutions with because it lacks debugging tools. I formerly used a text editor and the Nokia Toolkit browser to build WAP applications. But now the MIT provides me with Visual Basic–like drag-and-drop mechanisms to create mobile controls that are pretty flexible. These controls can be targeted to produce WML 1.1–compliant code or device-specific code. They can also render HTML 3.2 or cHTML code without any additional code or DLL references to your code. The MIT runtime is intelligent enough to detect the device type and push WML 1.1, cHTML, or HTML 3.2 to the target device.
Pick a Web Service
The first step in creating the WAP client is to pick a Web service to consume. I specifically picked a non-Microsoft Web service to illustrate the following:
- Web services are "basic building blocks" that are independent of platform implementation.
- The Web Service Description Language glues one Web service to another. WSDL 1.1 glues a Web service very well to a client that resides on another platform. This is a step towards seamlessly integrating data using XML standards irrespective of software platforms.
My current employment revolves around stock-brokering. Therefore, I chose to use the Web service from XMethods called "Delayed Stock Quotes," which gets me stock price quotes.
This service is built on a technology from Mind Electric called GLUE, which is their Java-based platform focused on distributed computing leveraging Web services. You can find more about GLUE on the Mind Electric site. This is a great example of Web services because I personally know very little about GLUE and have no coding experience with it. To be frank, I hadn't even heard of GLUE until I discovered this Web service. I used the XMethods Web service directory to find this service. (In the future this will be done using Universal Description, Discovery, and Integration—UDDI—which I'll discuss later in the series.)
You need the WSDL file location to create a proxy object, which I discussed in my last installment. You don't need any technical knowledge of the Web service, its platform, firewall, or security information. As long as the file is WSDL 1.1–compliant, the .NET framework is intelligent enough to create the proxy stub.
Because the proxy object needs to get access to your code, it should share the same namespace with your project classes. Therefore, you first have to create your project before you can create the proxy object. After the Visual Studio IDE (Integration Development Environment) creates a namespace with the same project name, you can insert your proxy object class into your namespace.
 |
|
Figure 1. Creating the mobile Web application project. (Click to enlarge.)
|
Create a New Project in Visual Studio.NET Studio
Select New > Project form the Visual Studio IDE and then choose Visual C# Projects from the Project Types menu. Because you are trying to create a mobile application, select the Mobile Web Application template. Type your project name in the Name box. I'll call this project "Stock_Clients" for the purpose of my example (see Figure 1).
This does the following:
- Creates a "Stock_Clients" directory under the wwwroot directory (example: d:\inetpub\wwwroot\Stock_Clients).
- Maps a virtual path from "localhost" to that directory with the same project name (example: http://localhost/Stock_Clients).
After you click OK, Visual Studio will create the project.
Create a Proxy Object and Add It to the Project
The namespace for your project is "Stock_Clients." You can create the proxy object now. Use the Wsdl.exe utility in the .NET Framework for this purpose. This command-line utility can also be found in the "bin" directory of the .NET framework installation directory (example: d:\Program Files\Microsoft.Net\FrameworkSDK\Bin\).
Open the MS-DOS/Command Prompt window and execute the following command:
wsdl.exe http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl
/n:Stock_Clients /o:d:\inetpub\wwwroot\Stock_Clients\Stocks_Proxy.cs
This command instructs the .NET runtime to do the following:
- Refer to a WSDL file at the http://services.xmethods.net/soap/urn:xmethods-delayed-quotes.wsdl location.
- Create the proxy class as Stocks_Proxy.cs (see Listing 1) in the d:\inetpub\wwwroot\Stock_Clients\ directory.
- Insert Stocks_Proxy.cs into the Stock_Clients namespace.
You can get a complete list of the Wsdl.exe arguments by referring to my previous article.
The above command will generate the following output:
Microsoft (R) Web Services Description Language Utility
[Microsoft (R) .NET Framework, Version 1.0.2914.16]
Copyright (C) Microsoft Corp. 1998-2001. All rights reserved.
Writing file 'd:\inetpub\wwwroot\Stock_Clients\Stocks_Proxy.cs'.
Now add "Stocks_Proxy.cs" to your project:
- Right-click on the Stock_Clients project name in the Solution Explorer.
- Select Add > Add Existing Item from the pop-up menus.
- Navigate to the Stocks_Proxy.cs file location (d:\inetpub\wwwroot\Stock_Clients\) and select the file.
Create the Mobile Forms
Now you need to create the interface. This can easily be accomplished by dragging the toolbox mobile Web controls onto the mobile Web forms.
 |
|
Figure 2. Creating mobile Web forms. (Click to enlarge.)
|
 |
|
Figure 3. Linking the two forms. (Click to enlarge.)
|
Create two mobile Web forms. The first form (Frm_Input) will let the user enter the stock code. The second form (Frm_Result) will display the price quote for the stock symbol. Each form will have some labels, text boxes, and link controls, similar to hypertext links on a Web browser (see Figure 2). Alternatively you can use a button.
Now you need to link the forms together so that users can navigate from one to the other:
- On the Frm_Input mobile form, right-click on the Get Quote link and get the properties list.
- Change the Navigation URL property to #Frm_Result (see Figure 3). This will navigate users to the Frm_Result Web form when they click on the Get Quote link.
Write Code That Consumes the Web Service
Now you need to write some code that consumes whatever this Web service offers. To do that, create an instance of the proxy object and invoke its methods. But how do you know the object's type and class name? Remember that the only information you know about a Web service is the location of the WSDL file. You are not aware of any semantics of the proxy class. Therefore, look closely at the "Stock_Proxy.cs" file.
Here is the class declaration for the file:
public class netxmethodsservicesstockquoteStockQuoteService :
System.Web.Services.Protocols.SoapHttpClientProtocol {
[System.Diagnostics.DebuggerStepThroughAttribute()]
public netxmethodsservicesstockquoteStockQuoteService() {
this.Url = "http://64.39.29.211:9090/soap";
}
You can see that the class name is "netxmethodsservicesstockquoteStockQuoteService," so now create the object type:
netxmethodsservicesstockquoteStockQuoteService proxyClass = new
netxmethodsservicesstockquoteStockQuoteService();
As soon as you create the "proxyClass" object, the .NET framework will make its interface available to you. All you have to do is press "proxyClass" to get a list of the available attributes and method calls.
Here is the code to invoke the "getQuote" method and request for a stock price feed (see Listing 2 for the full code):
private void Frm_Result_Activate(object sender, System.EventArgs e)
{
netxmethodsservicesstockquoteStockQuoteService proxyClass = new
netxmethodsservicesstockquoteStockQuoteService();
Lbl_Input_Code.Text = "Quote for " + Txt_Code.Text;
Lbl_Price.Text = "Price :- $" + proxyClass.getQuote(Txt_Code.Text).ToString();
}
  |
|
Figure 4. WML output on a WAP phone/emulator. (Click to enlarge.)
|
 |
|
Figure 5. WML code generated by the MIT. (Click to enlarge.)
|
 |
|
Figure 6. HTML 3.2 output generated by the MIT. (Click to enlarge.)
|
It's time to compile the project files. You can do this by selecting Build or Build All from the Build menu. Bring the mobile Web form (MobileWebForm1.aspx, see Listing 3) into a WAP toolkit. I used the Nokia WAP Toolkit 2.0 browser (see Figure 4).
Take a look at the WML deck. We generated none of the code; the .NET runtime converted mobile Web form controls to WML behind the scenes (see Figure 5).
Because the MIT also renders the output in HTML 3.2 format, you can view the same output in a Web browser (see Figure 6).
You can download the project files here (Stock_Clients.zip).
What's the future of .NET and handheld devices? Microsoft is currently working on something called the ".NET Compact Framework," which will be implemented on handheld mobile devices. This framework is intended to bring desktop application capabilities to future handhelds—which won't be simple organizers, but will have database capabilities and even run small Web servers. Imagine a small desktop SQL Server app directly porting to the Pocket PC operating system. These are some of the long-term goals of the .NET Compact Framework. In the meantime, the MIT will remain as the prime development environment for mobile applications.
In my next article, I will create a Web client and a Windows application client.
(Series to be continued...)