Apply Your Knowledge

   


Exercises

4.1 Calling a Web Service Asynchronously

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.

  1. Open a Visual Basic .NET Windows Application in the Visual Studio .NET IDE.

  2. Right-click the References folder in Solution Explorer and select Add Web Reference. This opens the Add Web Reference dialog box.

  3. 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.

  4. Click the Add Reference button.

  5. Add a new Form to your Visual Basic .NET Windows project.

  6. Place a TextBox control named txtInput , a Button control named btnUpperCase , and a TextBox control named txtOutput on the Form.

  7. 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 
  8. Set the Form as the startup object for the project.

  9. 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.

4.2 Using a Web Service from ASP.NET

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.

  1. Create a new Visual Basic ASP.NET Web Application project.

  2. Right-click the References folder in Solution Explorer and select Add Web Reference. This opens the Add Web Reference dialog box.

  3. 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.

  4. Click the Add Reference button.

  5. 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.

  6. 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 
  7. Set the Web Form as the start page for the project.

  8. 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.

    Figure 4.15. Invoking a Web service from an ASP.NET application.

Review Questions

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.

Exam Questions

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?

  1. Open the .asmx file in a Web browser.

  2. Run the XML Schema Definition Tool.

  3. Run the Web Service Discovery Tool.

  4. Copy the .asmx file to your client project.

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?

  1. Create an entirely new client application, and add a Web reference for the Web service to the new application.

  2. Delete and re-create the Web reference in the existing application.

  3. Update the Web reference in the existing application.

  4. Use a generic Object variable to hold customer information so that you can call any property you like.

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?

  1. The .asmx file for the Web service is not available on your Web server.

  2. The Web service class is not marked with the <WebService> attribute.

  3. The Cash method is not marked with the <WebMethod> attribute.

  4. Web services can only return string values.

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?

  1. Cut and paste the code into a Windows application project, and test it in the new project.

  2. Run the Web service project and use the test page that it opens in the browser.

  3. Use a tool such as WebService Studio to send SOAP requests directly to the server.

  4. Have a large number of beta testers use the application, and monitor the server for odd behavior.

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?

  1. Suppliers.DataSet

  2. DataSet

  3. Northwind.DataSet

  4. DataRow()

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?

  1. The server address that you typed does not exist.

  2. The server requires authentication, and you have entered improper credentials.

  3. The Web Services Discovery Tool works only on your local computer.

  4. There is no WSDL or Disco file available at the address that you typed.

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?

  1. Use the /namespace option of the Web Services Description Language Tool to specify a unique namespace for the new class.

  2. Rename the existing class.

  3. Use the /out option of the Web Services Description Language Tool to specify a unique output file name for the new class.

  4. Manually edit the generated proxy class to change the classname that it contains.

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?

  1. Use the Web Service Discovery Tool to download the WSDL file for the Web service and inspect it in an XML editor.

  2. Use the Web Service Description Language Tool to create a proxy class for the Web service and inspect the class using a text editor.

  3. Craft SOAP messages in an XML editor and use them to test the Web service.

  4. Use a tool such as the .NET WebService Studio to exercise the interface of the Web service.

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?

  1. Move the application to a faster computer.

  2. Install a faster link to the Internet.

  3. Install more memory in the computer.

  4. Use asynchronous calls to invoke the Web service.

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?

  1. Use the Web Service Discovery Tool to download the information.

  2. Use the Web Service Description Language Tool to create a proxy class.

  3. Use a UDDI Registry to locate the Web service.

  4. Use a search engine to explore your partner's Web site.

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?

  1. Nothing. The client can always call a Web service asynchronously.

  2. Use a separate Thread object for each invocation of the Web service.

  3. Provide callback functions to invoke the Web service.

  4. Use only Sub procedures rather than Function procedures as Web methods.

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?

  1. The client project and the Web service project must use the same language.

  2. Objects supplied by a Web service cannot cross a firewall.

  3. The client project does not contain a reference to the System.Data namespace.

  4. Web services cannot properly serialize a complex object such as the DataSet object.

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?

  1. Create a new DataSet object and use the ReadXml method of the DataSet to initialize it from the returning SOAP message.

  2. Obtain an XSD file that specifies the schema of the DataSet . Use this XSD file to instantiate a DataSet from the returned data from the GetOrders method.

  3. Assign the return value from the GetOrders method to an array of DataRow variables . Loop through the array to build the DataSet .

  4. Assign the return value from the GetOrders method to a DataSet variable.

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?

  1. disco.exe

  2. results.discomap

  3. ZipcodeService.wsdl

  4. ZipcodeService.disco

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#?

  1. Manually convert the C# code to VB .NET code.

  2. Rerun the tool, specifying the /language:VB option.

  3. Rerun the tool, specifying the /namespace:VB option.

  4. Select File, Save As and save the file with the .vb extension.

A15:

B. The /language option controls the output language of the wsdl.exe tool.

Suggested Readings and Resources

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


MCAD. MCSD Training Guide (Exam 70-310. Developing XML Web Services and Server Components with Visual Basic. NET and the. NET Framework)
MCAD/MCSD Training Guide (70-310): Developing XML Web Services and Server Components with Visual Basic(R) .NET and the .NET Framework
ISBN: 0789728206
EAN: 2147483647
Year: 2002
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net