Asynchronous Consumption of Web Services


When you are building your applications that consume SOAP messages from Web services out there in the world, you may notice that working with these Web services synchronously can be slow and inefficient for applications that depend upon good performance.

In most cases, you are not in control of the actual Web services that you are consuming. Some Web services might return a SOAP response to you quickly, whereas others might return a response very slowly-to the detriment of your consuming application.

Typically, when you have worked with Web services, you have done so in a synchronous manner. When you make synchronous invocations of any Web service, your application simply sends a SOAP request and waits for a response before any application processing continues. While the consuming application is waiting for the response from the Web service, the consuming application is locked to the end user. This process is shown in Figure 19-20.

image from book
Figure 19-20

To improve efficiency, you can work with slower Web services asynchronously rather than synchronously. An asynchronous invocation of a Web service is the opposite of a synchronous invocation. When your consuming application makes an asynchronous invocation of a Web service, it can work on other things while it waits for the response from the Web service. After working on something else, your application can then return to receive the response. Figure 19-21 shows you what an asynchronous invocation of a Web service looks like.

image from book
Figure 19-21

Building a Slow Web Service

How can you build your Web services to be consumed asynchronously? Well, nothing really. The asynchronous communication that might be required is provided for on the consuming side of the equation. However, for an example of this, you first have to have a slow Web service in place. This slow Web service (shown in Listing 19-14) uses a for loop to do a small count before returning a result.

Listing 19-14: Building a slow Web service

image from book
      using System;      using System.Web;      using System.Collections;      using System.Web.Services;      using System.Web.Services.Protocols;      [WebService(Namespace = "http://www.wrox.com/")]      [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]      public class SlowBoy : System.Web.Services.WebService {          public SlowBoy () {          }          [WebMethod]          public int TakeLongTime() {              int x = 0;              for (int y = 1; y <= 1000; y++)              {                  y += 1;                  x = y;              }              return x;          }      } 
image from book

You can see that this Web service simply runs through a loop 1000 times before sending back the value of 1000-all done through the wonderful name of TakeLongTime(). Now, the next step is to consume this Web service asynchronously from a consuming application.

Consuming the TakeLongTime() WebMethod Asynchronously

If you look at the WSDL document for the SlowBoy Web service, you see a definition is in place for the TakeLongTime() WebMethod. However, when you build a .NET application that consumes this Web service, notice that you have access to two other methods: BeginTakeLongTime() and EndTakeLongTime(). These Begin and End methods are created for you with no action on your part required. You need these two methods to invoke the Web service in an asynchronous manner.

To see an example of using the BeginTakeLongTime() and the EndTakeLongTime() methods in your client application, take a look at the code in Listing 19-15. This ASP.NET application sends off a SOAP request to the Web service. Then, while the request is being processed by the Web service, the client application continues its own processing by running a counter until the Web service is ready to return a SOAP response back. When the Web service is ready to provide a response, this response is retrieved and both numbers are displayed to the browser screen. For this example, create a simple ASP.NET page that contains two Label controls that display the output-Label1 and Label2.

Listing 19-15: Asynchronous invocation of a Web service

image from book
      <%@ Page Language="C#" %>      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">      <script runat="server">          protected void Page_Load(object sender, EventArgs e)          {              AsyncTest.SlowBoy ws = new AsyncTest.SlowBoy();              IAsyncResult asyncCheck = ws.BeginTakeLongTime(null, null);              int x = 0;              while (asyncCheck.IsCompleted == false)              {                  x += 1;              }              Label1.Text = x.ToString();              Label2.Text = ws.EndTakeLongTime(asyncCheck).ToString();          }      </script>      <html xmlns="http://www.w3.org/1999/xhtml" >      <head runat="server">          <title>Asynchronous invocation</title>      </head>      <body>          <form  runat="server">          <div>              <asp:Label  runat="server" Text="Label"></asp:Label><br />              <br />              <asp:Label  runat="server" Text="Label"></asp:Label>          </div>          </form>      </body>      </html> 
image from book

Because the developer of the consuming application anticipates a long wait if it directly invokes the TakeLongTime() method, another option is for the application to instead invoke the BeginTake LongTime() method. By using the BeginTakeLongTime() method, the client application retains control-instead of being forced to wait around to get a response from the Web service. The client application is then free to do whatever it wants before returning to retrieve the result from the Web service.

After the SOAP request is sent to the Web service, the client application can use the IAsyncResult instance to check whether the method call has been completed. In this case, the client application checks whether the method call has been completed by using asyncCheck.IsCompleted. If the asynchronous invocation is not complete, the client application increases the x variable by one before making the check again. The client application does this until the Web service is ready to return a response. The example of the result returned to the browser is illustrated in Figure 19-22.

image from book
Figure 19-22

As you can tell, this powerful capability can add a lot to the performance of your consuming applications.




Professional XML
Professional XML (Programmer to Programmer)
ISBN: 0471777773
EAN: 2147483647
Year: 2004
Pages: 215

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