It is possible that you need to make multiple Web service calls from multiple Web services in order to load a page. Next I’ll shows you how to do that with the asynchronous PreRequestHandler execution approach. It is similar to do that with the asynchronous page approach.
Assume that I have two Web services: Service1 and Service2. Service1 has two methods: SlowMethod1 and FastMethod1. Service2 has two methods: SlowMethod2 and FastMethod2. To generate a page, I need to make calls to all the four methods. I’ll use the same four steps. I’ll highlight and mention those differences.
Step 1. Creating a class implementing IAsyncResult and a custom proxy class
Each proxy object will have a HashTable to store a bunch of IAsyncResult interfaces for calls on this proxy. Each key in the HashTable can also be used as the key for the result of the call stored in the request context.
Each MyAsyncResult object will have an ArrayList to store the references to the proxies. Each Global object will also have a count to track if all the calls are ready for harvest results. I increment the count each time when I make a Web service call and decrement the count each time when MyCallBack is invoked.
Step 2. Creating a BeginEventHandler method, an EndEventHandler method, and an intermediate MyCallback
Since the MyCallBack method calls might run on the different threads, multiple threads might access the count of my MyAsyncResult object. We use the lock statement to control access to the MyAsyncResult object proxy1.Res.
A unique MyAsyncResult object proxy1.Res is used in the BeginPreRequestHandlerExecute method because we make a bunch of asynchronous calls and the BeginPreRequestHandlerExecute method returns a single IAsyncResult interface. That is the reason why we wrap all the proxies in one MyAsyncResult object and each proxy object will have a HashTable to store a bunch of IAsyncResult interfaces for calls on this proxy.
When count equals to 0 in the MyCallback method, I harvest the response data for all the calls and make the Res.cb.Invoke call to relay the process.
Step 3. Calling the AddOnPreRequestHandlerExecuteAsync method
There is no change made.
Step 4. Using the response data
Using the response data is trivial.