GUI applications are known as Windows applications in Visual Studio .NET. The big advantage of a GUI is that you can show much more data to the user at once. In addition to text, you can harness the full power of graphics to compartmentalize data or translate that information into whatever makes the most sense. By now, most developers are familiar with the advantages of a GUI—easy menu navigation, event-driven programming, richer user interaction models, and so on. Because all of these advantages exist, it only makes sense to be able to access Web Service here as well. This time around, we will access the FirstService.GetRandomNumbers Web Method. To begin, we must first create a Windows Application project. Perform the following steps to create the Windows application:
1. |
Select File, Project, New Project. |
2. |
Select the following items in the New Project dialog:
|
This time around we have the skeleton for a GUI application. The IDE displays a blank form, Form1. On this form, we want to place controls for the following items:
A place for the user to enter the number of random numbers he or she wants.
A place for the user to enter the number of minimum value of the random numbers.
A place for the user to enter the number of maximum value of the random numbers.
A place to display the results to the user.
Figure 2.7 shows my proposed layout of the form.
After placing the controls on the form, I changed their names to make their data association clear. Our next step is to generate the proxy for the FirstService Web Service. Add a Web reference to http://localhost/Chapter1/Chapter1.vsdisco and rename the namespace from localhost to Chapter1. With this, we are almost done. We just have to write the code that executes when the Get Values button is clicked. The fastest and easiest way to do this is to double-click the Get Values button in the form designer. This will create the skeleton for the click event handler and bring up the code view.
Within the code, we want to make sure that all of the client input is valid. If all of the input is valid, we will call the Web Service and display the output. Listing 2.3 shows the full source for click handler:
Most of the code in here is fairly straightforward. It checks what the user typed in for the various values and lets him or her know if anything is wrong. The Web Service-specific code does not occur until the last few lines of code.
The first thing you might notice is that the call to GetRandomNumbers is enclosed in a Try...Catch...Finally block. This is a brand new construct in Visual Basic .NET and is meant to supercede the On Error Goto syntax of its predecessors. All calls to Web Services can throw exceptions. Sometimes, it will be because of a connection failure, bad input, a SOAP Fault generated by the server, or some other item. Because an exception might leave the proxy with an open connection, it is very important to wrap the SOAP call in a Try...Catch...Finally block. The Finally part of the block should call the proxy's Dispose function. Doing so will release any resources and close the connection to the server. You will note that we did not do this in the CLI example. Now might be a good time to go back to that example and properly handle the call to the Web Service.
With this code in place, let's experiment with the application itself. Build the application (go to the Build menu and select Build). Then, select Debug, Start. With the application running, experiment and see how it works. Figure 2.8 shows the results of one run on my machine.
Before going on to the next example, we will also look at what the SOAP message exchange looked like. You may want to compare this with the request and response done with the HTTP/GET Web request listed in Chapter 1. The differences are minimal at best.
Request:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetRandomNumbers xmlns="http://tempuri.org/"> <arraySize>10</arraySize> <min>1</min> <max>10</max> </GetRandomNumbers> </soap:Body> </soap:Envelope>
Response:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetRandomNumbersResponse xmlns="http://tempuri.org/"> <GetRandomNumbersResult> <long>6</long> <long>10</long> <long>9</long> <long>7</long> <long>8</long> <long>1</long> <long>10</long> <long>10</long> <long>6</long> <long>2</long> </GetRandomNumbersResult> </GetRandomNumbersResponse> </soap:Body> </soap:Envelope>
Enough work on the desktop. Let's try out some Web-based access next.