Depending on the speed of your Internet connection, you might have noticed that the Web service client applications you constructed earlier in this chapter take a long time to reload when you invoke the Web service. That's because by default these applications use synchronous methods to communicate with the Web service, waiting for the SOAP response before allowing any other code to execute. But the proxy classes constructed by .NET include asynchronous methods as well. In this exercise, you'll learn how to call a Web service asynchronously.
Estimated Time : 30 minutes.
Open a Visual Basic .NET Windows Application in the Visual Studio .NET IDE.
Right-click the References folder in Solution Explorer and select Add Web Reference. This opens the Add Web Reference dialog box.
Type http://hourglass/StringProc/Strings.asmx?wsdl into the Address bar of the Add Web Reference dialog box and press Enter (of course, replace hourglass with the name of your own server). This connects to the StringProc Web service and creates the appropriate proxy classes, as you learned in this chapter.
Click the Add Reference button.
Add a new Form to your Visual Basic .NET Windows project.
Place a TextBox control named txtInput , a Button control named btnUpperCase , and a TextBox control named txtOutput on the Form.
Double-click the Button control to open the Form's module. Enter this code to invoke the Web service when the user clicks the button. You'll need to replace hourglass with the name of your own server:
Private strUpperCase As String Private Sub btnUpperCase_Click(_ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnUpperCase.Click ' Connect to the Web service Dim objStrings As hourglass .Strings = _ New hourglass .Strings() ' Invoke the Web service. ' This may take some time ' so call it asynchronously. ' First, create a callback function Dim wcb As New AsyncCallback(_ AddressOf WebServiceCallback) ' And then initiate the ' asynchronous call objStrings.BeginToUpper(_ txtInput.Text, wcb, objStrings) End Sub Public Sub WebServiceCallback(_ ByVal ar As IAsyncResult) ' This function will get called ' when the Web service call is done ' Retrieve the state of ' the proxy object Dim objStrings as _ hourglass.Strings = ar.AsyncState ' Call the End method ' to finish processing txtOutput.Text = _ objStrings.EndToUpper(ar) End Sub
Set the Form as the startup object for the project.
Run the project and fill in a value for the input string. Click the button. Wait a few moments, and the output box will show the uppercase version of the input string. Note that while you're waiting, you can still drag the form around the screen, which shows that it is not blocked by the Web service call.
If you compare the code for this exercise with the code that you saw in Step By Step 4.1, you'll find some significant changes. In the .NET Framework, asynchronous Web service calls are managed by callback functions. When you add a Web reference, the proxy class includes Begin and End methods for each Web method. In this case, those are the BeginToUpper and EndToUpper methods.
The Begin method takes all the same parameters as the underlying Web method, plus two others. The first is the address of a callback function, and the second is an object whose properties should be available in the callback function. When you call the Begin method, the .NET Framework launches the call to the Web service in the background. When the Web method call completes, the Callback function will be invoked. The code in this exercise shows how you can then retrieve the original object and use its End method to finish the work of using the Web service.
Although the examples in this chapter were written as Windows applications, Web services work equally well when called from ASP.NET Web applications. In this exercise, you'll use ASP.NET to retrieve information from the Airport Weather Web service.
Estimated Time : 25 minutes.
Create a new Visual Basic ASP.NET Web Application project.
Right-click the References folder in Solution Explorer and select Add Web Reference. This opens the Add Web Reference dialog box.
Type >http://live.capescience.com/wsdl/AirportWeather.wsdl into the Address bar of the Add Web Reference dialog box and press Enter. This connects to the Airport Weather Web service and creates the appropriate proxy classes, as you learned in this chapter.
Click the Add Reference button.
Place a Label control, a TextBox control with the ID of txtCode , a Button control with the ID of btnGetSummary , and a ListBox control with the ID of lbResults on the default Web Form in the project.
Double-click the Button control to open the Web Form's module. Enter this code to invoke the Web service when the user clicks the button:
Private Sub btnGetSummary_Click(_ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles btnGetSummary.Click ' Connect to the Web ' service by declaring ' a variable of the appropriate type Dim aw As com.capescience. _ live.AirportWeather = _ New com.capescience.live. _ AirportWeather() ' Call the Web service ' to get the summary ' for the entered airport Dim ws As com.capescience. _ live.WeatherSummary = _ aw.getSummary(txtCode.Text) ' Display some of the properties ' filled in by the Web service With lbResults.Items .Clear() .Add(ws.location) .Add("Temperature: " & ws.temp) .Add("Visibility: " & _ ws.visibility) .Add("Wind: " & ws.wind) End With End Sub
Set the Web Form as the start page for the project.
Run the project and fill in an airport code. Click the button. Wait a few moments, and the listbox will show the details of the weather for that airport, as shown in Figure 4.15.
1: | What is the purpose of a Web service proxy class? |
A1: | A Web service proxy class is an object that you can create on the client to communicate with a Web service. The proxy accepts messages, forwards them to the Web service, and returns the results of those messages. |
2: | Describe the general purpose of SOAP. |
A2: | SOAP is designed to encapsulate objects as XML messages. These objects can then be sent via HTTP or other standard communications channels. In addition, the SOAP format can be read by software on many platforms. |
3: | Describe the general purpose of Disco and UDDI. |
A3: | Disco and UDDI are designed to help you discover the interface details of a Web service. |
4: | Describe the general purpose of WSDL. |
A4: | WSDL exists to supply information on the interface of a Web service. |
5: | Can a Web service exist without a WSDL file? |
A5: | A Web service can exist without a WSDL file, but you must then know the exact incoming SOAP message that the Web service expects before you can use it. |
6: | Explain two ways in which you can create proxy classes for a Web service. |
A6: | You can create proxy classes for a Web service by using the disco.exe and wsdl.exe tools or by creating a Web reference within Visual Studio .NET. |
7: | List three steps involved in building a Web service using Visual Studio .NET. |
A7: | To build a Web service, you must create a new Web service application, mark the classes to be exposed with the <WebService> attribute, and mark the methods to be exposed with the <WebMethod> attribute. |
8: | What tools can you use to make local copies of the configuration files for a Web service? |
A8: | The disco.exe tool will make local copies of the configuration files for a Web service. Creating a new Web reference will also create these files. |
9: | How can you test a Web service without building a client application? |
A9: | You can use a tool such as .NET WebService Studio to test a Web service without building a client application. |
10: | What is the advantage of sending SOAP messages over the HTTP protocol? |
A10: | Using HTTP as the transport protocol for SOAP messages means that these messages can take advantage of pervasive Internet connectivity to reach their destination. |
1: | You want to use a Web service that supplies inventory level information in your application. You know the URL of the .asmx file published by the Web service. What step should you take first?
|
A1: | C. The Web Service Discovery Tool retrieves copies of the files that you need to proceed with this project. |
2: | Your application includes a Web reference to a Web service that delivers customer information as an object with multiple properties. The developer of the Web service has added a new property named CreditRating to the object. What should you do to be able to use the CreditRating property in your code?
|
A2: | C. The Update Web Reference menu item for a Web reference refreshes local configuration information from the server that hosts the Web service. |
3: | You have created a Web service to return financial information using ASP.NET. One of the methods in your Web service is defined with this code: Public Function Cash() As Double ' Calculations omitted End Function Potential consumers of your Web service report that although they can set a reference to the Web service, the Cash method is not available. What could be the problem?
|
A3: | C. All exposed methods of a Web service must be marked with the <WebMethod> attribute. |
4: | You have created a new Web service to perform financial calculations. You're working in an ASP.NET project within the Visual Studio .NET environment. What's the easiest way to test your new Web service to make sure that it's returning the proper results?
|
A4: | B. When you're creating a Web service in ASP.NET, running the project will open a testing form in a browser window. |
5: | Your application uses a Web service named Northwind. The Northwind Web service includes a Web method named Suppliers that returns a DataSet containing all the company's suppliers. What data type should you use to declare an object to hold the result of the Suppliers method?
|
A5: | B. The client needs to declare the same data type that the server is returningin this case, the DataSet object. |
6: | You're using the Web Services Discovery Tool to determine information about a Web service on a particular server. You receive the error message "The HTML document does not contain Web service discovery information." What could be the problem?
|
A6: | D. The Web Services Discovery Tool requires the URL to a Disco or WSDL file to function. |
7: | You are using the Web Services Description Language Tool to create a proxy class for a Web service. The Web service exposes a class named Customer . You already have a Customer class in your application. What should you do to allow both classes to coexist in the same application?
|
A7: | A. Specifying a unique namespace for the new object removes the chance that it can clash with a pre-existing object name. |
8: | You have used a UDDI registry to locate a Web service that might be able to supply information for your business. You want to test the interface of the Web service to make sure that it meets your requirements before you invest the effort to build a client application. How should you proceed?
|
A8: | D. By using an automated tool, you can avoid tedious and error-prone inspection of the XML files. |
9: | Your application calls a Web service that performs complex, time-consuming calculations. Users complain that the user interface of the application freezes while it's recalculating. What can you do to fix this problem?
|
A9: | D. Speeding up the client computer will do nothing to speed up the Web service, which runs on the server computer. |
10: | One of your partner businesses has informed you that it is making its inventory information available via a Web service. You do not know the URL of the Web service. How can you discover the URL?
|
A10: | C. UDDI Registries exist so that you can find business services by browsing or searching. |
11: | What must a developer do to make a Web service built with Visual Studio .NET available asynchronously?
|
A11: | A. Building the proxy class, either with wsdl.exe or by setting a Web reference, automatically creates methods to invoke the Web service asynchronously. |
12: | You are invoking a Web service that returns a DataSet object. Your client application is written in Visual Basic .NET, whereas the Web service itself is written in C#. The Web service is outside of your corporate firewall. You receive an "object not found" error when you call the method that returns the DataSet . What could be the problem?
|
A12: | C. Web services client and server applications must agree on the definition of the data to be exchanged. |
13: | Your application invokes a Web service named Northwind that includes a Web method named GetOrders . GetOrders returns a DataSet containing order information. What must you do to use this DataSet in your client application?
|
A13: | D. The only thing you need to do to use a complex variable returned by the Web service is to declare an instance of the same data type in the client application. |
14: | You have used the Web Services Discovery Tool to retrieve information about a Web service named ZipcodeService. Which file will contain the URL for any documentation of the ZipcodeService Web service?
|
A14: | D. The Disco file is the only one that contains pointers to non-XML resources. |
15: | You have used the Web Services Description Language Tool to create a proxy class for a Web service. When you add the proxy class to your project, you discover that it is coded in the C# language. What must you do to get the proxy class in VB .NET instead of C#?
|
A15: | B. The /language option controls the output language of the wsdl.exe tool. |
1. Visual Studio .NET Combined Help Collection
2. .NET Framework SDK Documentation
XML Web Services Created Using ASP.NET and XML Web Service Clients
3. Scribner, Kenn and Stiver, Mark C. Applied Soap: Implementing .NET XML Web Services . Sams, 2001.
4. Cerami, Ethan. Web Services Essentials . O'Reilly, 2002.
5. Short, Scott. Building XML Web Services for the Microsoft .NET Platform . Microsoft Press, 2000.
6. Basiura, Russ, et al. Professional ASP.NET Web Services . Wrox, 2001.
Top |