Creating Asynchronous Web Methods

If you look at the proxy class that was created in the SquareRootClient projects, you will see that it offers more than a simple, synchronous method call for each of the methods exposed by the XML Web service. For each of the web service methods, there is also a set of proxy methods called Beginmethodname and Endmethodname. These methods enable you to use asynchronous callbacks—that is, your application can make a Web service request and continue with its own activities, without having to wait for the Web service request to complete.

When calling XML Web services over the Internet, you might find that the response time can vary. Instead of having your client application wait for the results to be returned from the web service and appear to be unresponsive to the user, use the asynchronous calls to enable your user interface to remain responsive and provide status information to the user. Listing 4.4 shows the methods from the proxy class that provides synchronous and asynchronous access to the GetSquareRoot method of your SquareRootService.

Listing 4.4: The Methods That Are Automatically Generated in the Proxy Class

start example
<System.Web.Services.Protocols.SoapDocumentMethodAttribute( _   "http://tempuri.org/GetSquareRoot", _   RequestNamespace:="http://tempuri.org/", _   ResponseNamespace:="http://tempuri.org/", _   Use:=System.Web.Services.Description.SoapBindingUse.Literal, _   ParameterStyle:=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)> _   Public Function GetSquareRoot(ByVal inputVal As Double) As Double      Dim results() As Object = Me.Invoke("GetSquareRoot", _          New Object() {inputVal})             Return CType(results(0),Double) End Function Public Function BeginGetSquareRoot(ByVal inputVal As Double, _   ByVal callback As System.AsyncCallback, _   ByVal asyncState As Object) As System.IasyncResult     Return Me.BeginInvoke("GetSquareRoot", _         New Object() {inputVal}, callback, asyncState) End Function Public Function EndGetSquareRoot(ByVal asyncResult As _   System.IAsyncResult) As Double     Dim results() As Object = Me.EndInvoke(asyncResult)         Return CType(results(0),Double) End Function
end example

The code in Listing 4.5 shows how to use a .NET Framework class called AsyncCallback with the BeginGetSquareRoot and EndGetSquareRoot methods that are included in the proxy class.

Listing 4.5: Calling an XML Web Service Method Asynchronously

start example
Private Sub asyncSquare()     Dim objSquare As SquareRootService.Square         objSquare = New SquareRootService.Square()     Dim inputValue As Double     sBar.Text = "Beginning async Web Service call . . ."     inputValue = CType(txtValue.Text, Double)     'create the callback delegate     Dim myCallBack As AsyncCallback     myCallBack = New AsyncCallback(AddressOf Me.GetResult)     objSquare.BeginGetSquareRoot(inputValue, myCallBack, objSquare) End Sub Private Sub GetResult(ByVal ar As System.IAsyncResult)     Dim webResult As Double     Dim objSquare As SquareRootService.Square = _         CType(ar.AsyncState, SquareRootService.Square)     webResult = objSquare.EndGetSquareRoot(ar)     sBar.Text = "Returned from async Web Service call . . ."     txtResult.Text = webResult.ToString End Sub
end example

Here we have two procedures. The first one, asyncSquare, is responsible for calling the BeginGetSquareRoot method from the proxy class and setting up the AsyncCallback delegate. When instantiating an AsyncCallback object in Visual Basic .NET, the object’s constructor requires that the AddressOf operator is used to assign a reference to the procedure that will be called when the BeginSquareRoot method is complete. If you look at the code in the BeginGetSquareRoot method in the proxy class, you see that all it is doing is calling GetSquareRoot and passing along the reference to the AsyncCallback object. When the BeginSquareRoot method is complete, execution goes to the GetResult method. GetResult receives state information about the currently executing asynchronous operation and completes it by calling EndGetSquareRoot. The EndGetSquareRoot method is responsible for calling EndInvoke on itself and passing the results back to the client code.

In Exercise 4.4, you will test the asynchronous method call by modifying the project that you completed in Exercise 4.2.

Exercise 4.4: Calling an XML Web Service Method Asynchronously

start example
  1. Open the Visual Studio .NET project, called SquareRootClientProject, that you created in Exercise 4.2.

  2. Create two new procedures in the code for frmSquares.vb by using the code in Listing 4.5.

  3. Comment out the code that is currently in the btnRoot_Click subprocedure and add a call to the asyncSquare procedure, as shown in this code snippet:

    Call asyncSquare()
  4. Save and test your work. Set a breakpoint in the GetResult procedure and verify that it is hit when the response from the Web service is completed.

end example

Calling Web methods asynchronously adds an important level of control and sophistication to your applications. In the next section you will learn how to further extend your Web services and client applications by creating custom SOAP headers to send additional information along with your method call, and by using SOAP Extensions to cause procedures to run each time a SOAP message is sent or received.



MCAD/MCSD(c) Visual Basic. NET XML Web Services and Server Components Study Guide
MCAD/MCSD: Visual Basic .NET XML Web Services and Server Components Study Guide
ISBN: 0782141935
EAN: 2147483647
Year: 2005
Pages: 153

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