How to consume Amazon.com web service asynchronously using WCF

In my previous post I described with code sample how to consume Amazon.com web service using WCF client application. That sample called ItemSearch method synchronously. Meaning it waited for call to complete and return results. But some time your call may not be luck to return right away. You can take advanatage of asynchronous calls to web service and wait for response to return. While you are waiting for response, you can perform some other tasks.

You do not have to do anything manually to generate code for async request-response from Amazon.com web service. When you use Add Service Reference wizard to add service reference, click on Advanced button and then choose to generate async calls. See the following snapshots to see how it is to be done.

 

Here is the code that demonstrates how aync call is made to ItemSearchAsync method and then we have a EventWaitHandle on which application is waiting to be set.


using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Threading;
using AmazonClient.AmazonData;
namespace AmazonClient
{
 class Program
 {
   static void Main(string[] args)
   {
    System.ServiceModel.BasicHttpBinding httpBinding = new BasicHttpBinding();
    httpBinding.Name = "AWSECommerceServiceBinding";
    BasicHttpSecurity security = httpBinding.Security;
    security.Mode = BasicHttpSecurityMode.None;
    EndpointAddress addr = 
       new EndpointAddress("http://soap.amazon.com/onca/soap?Service=AWSECommerceService");
    AWSECommerceServicePortTypeClient client = 
       new AWSECommerceServicePortTypeClient(httpBinding,  addr);
    try
    {
       ItemSearch srch = new ItemSearch();
       srch.AssociateTag = "netomatix-20";
       srch.AWSAccessKeyId = "1QEW6V1V3C5GK7WB2A02";
       ItemSearchRequest req = new ItemSearchRequest();
       req.Keywords = "silverlight";
       req.SearchIndex = "Books";
       srch.Request = new ItemSearchRequest[1] { req };
       EventWaitHandle searchCompletionEvent = 
         new EventWaitHandle(false, EventResetMode.ManualReset);
       client.ItemSearchCompleted += 
         new EventHandler(ItemSearchCompleted);
       try
       {
	 client.ItemSearchAsync(srch, searchCompletionEvent);
         searchCompletionEvent.WaitOne();
       }
       catch (TimeoutException timeOutEx)
       {
        Console.WriteLine("Request timed out: {0}", timeOutEx.Message);
       }
       catch (FaultException faultEx)
       {
	Console.WriteLine("SOAP fault: {0}", faultEx.Message);
       }
       catch (CommunicationException commEx)
       {
	Console.WriteLine("Failed to communicate with web server: {0}", 
          commEx.Message);
       }
       Console.WriteLine("Thank you for choosing ByteBlocks!");
     }
     catch(Exception ex)
     {
	Console.WriteLine(ex.Message);
     }
  }

  static void ItemSearchCompleted(object sender, 
                              ItemSearchCompletedEventArgs e)
  {
   ItemSearchResponse resp = e.Result;
   EventWaitHandle waitHandle = e.UserState as EventWaitHandle;
   waitHandle.Set();
  }
 }
}

I passed EventWaitHandle as state object to async call. And then in completion event, I used that event to set the signal for waiting thread.

Search

Social

Weather

9.3 °C / 48.7 °F

weather conditions Clear

Monthly Posts

Blog Tags