Home / Articles / Programming Languages / Web Services / Asynchronous Web Services Client / Asynchronous Approach

“Chishiki” is Japanese for “knowledge.” e-chishiki.com aims to bring software developers, information security professionals, IT executives and other IT pros a rich body of knowledge in the form of articles, interviews, tutorials and technical discussions. Our contributors are among the biggest names in the Indian IT industry and include noted authors, educators and practitioners.

Weekly Programming Series - A Web Services Primer

A Web Services Primer (4/10): Asynchronous Web Services Client

Yashavant Kanetkar and Asang Dani
Yashavant Kanetkar and Asang Dani


Asynchronous Approach

APIs (1) and (2) mentioned above are implemented in the proxy class to facilitate asynchronous calls. When we call these APIs, control returns immediately, without waiting for the result of the operation. Once the control returns, the user is free to interact with the WinForm. A lot of things happen under the hood to facilitate this. These steps are outlined in Figure 2. Take a close look at it, as the explanation that follows refers to it.

Figure 2 – Asynchronous call to web service API

Given below is the sequence of steps that are carried out when the client uses the asynchronous APIs :

1. doConversion( ) function calls the API GetRateBySymbolAsync( ).

2. GetRateBySymbolAsync( ) calls base class function InvokeAsync( ). Note that the proxy class is derived from SoapHttpClientProtocol class present in FCL. While making the call, three parameters are passed to it:

  • Name of the web service API – GetRateBySymbol.
  • Array of the arguments for this API – ( fromC, toC ).
  • Address of callback function OnGetRateBySymbolOperationCompleted( ). Role of this function is explained in step.

3. InvokeAsync( ) API the heart of asynchronous approach. It performs two jobs:

  • It registers the address of callback function passed to it for this request.
  • It queues the request for execution of the web service API GetRateBySymbol.

4. The control returns from InvokeAsync( ) back to GetRateBySymbolAsync( ). and finally to doConversion( ).

5. In addition to the GetRateBySymbol, there might be requests for other web service APIs in the same quest. Requests in the queue are executed in the context of separate worker threads. Note that these worker threads themselves use synchronous approach to communication with the server. Although we can make intelligent guesses about how this works, implementation details are hidden inside InvokeAsync( ). Server is completely unaware of the fact that these requests are being made by an asynchronous client.

6. As each request is completed, the callback function registered for that request is invoked. Result of the API call is passed as an argument to the callback function.

7. In our case, the callback function is OnGetRateBySymbolOperationCompleted( ). It generates an event to notify the client about completion of asynchronous API call and availability of results. The result of the operation are passed to the event handler using object of class GetRateBySymbolCompletedEventArgs. This class is also generated by wsdl.exe. Our event handler convComplete( ) displays the result of operation in ResultBox textbox.

Note that, InvokeAsync( ) is a common function for asynchronous invocation for every web service API. It registers the callback and queues the API request for each API in the web service.

Comments

Log in or create a user account to comment.

On Sale From April 2008

Let Us C
8th Ed.
C programming classic & best seller. 1 million+ copies sold!

Y. Kanetkar

On Sale From April 2008

Introduction to Object Oriented Programming & C++

Y. Kanetkar

On Sale From Fall 2008

Microsoft .NET Framework: Web Application Security

Vijay Mukhi

On Sale From Nolvember 2008

Quest C++ Courseware
12+ hours of instructional audio and animated slides.

Y. Kanetkar Asang Dani

On Sale From November 2008

A Programmer's Guide to Web Application Security

Vijay Mukhi

Latest Forum Posts