title: Amazon Product Advertising API ItemSearch Returning Null
body: <p>I have spent entire day trying to fix problems with the Amazon ECS(Ecommerce Services) API.</p>
  
  <p>I have downloaded the example on their site for sending SOAP Requests with .NET 4.0 and WCF</p>
  
  <p><a href="http://aws.amazon.com/code/Product-Advertising-API/3941" rel="nofollow">http://aws.amazon.com/code/Product-Advertising-API/3941</a></p>
  
  <p>I have not changed anything in the code from the example, except for the AccessKeyID and SecretyKeyID in the config file. </p>
  
  <p>The Calling code looks like the following:</p>
  
  <pre><code>        // Instantiate Amazon ProductAdvertisingAPI client
          AWSECommerceServicePortTypeClient amazonClient = new AWSECommerceServicePortTypeClient();
  
          // prepare an ItemSearch request
          ItemSearchRequest request = new ItemSearchRequest();
          request.SearchIndex = "Books";
          request.Title = "WCF";
          request.ResponseGroup = new string[] { "Medium"};
  
          ItemSearch itemSearch = new ItemSearch();
          itemSearch.Request = new ItemSearchRequest[] { request };
          request.Condition = Condition.All;
          itemSearch.AssociateTag = "";
          itemSearch.AWSAccessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
  
          // send the ItemSearch request
          ItemSearchResponse response = amazonClient.ItemSearch(itemSearch);
          if (response != null)
          {
              // write out the results from the ItemSearch request
              foreach (var item in response.Items[0].Item)
              {
                  Console.WriteLine(item.ItemAttributes.Title);
              }
          }
          Console.WriteLine("done...enter any key to continue&gt;");
          Console.ReadLine();
  </code></pre>
  
  <p>the Call to the ItemSearch() is returning a null object. I looked further into this and saw that in the AmazongSigningMessageInspector Class, the AfterReceiveReply() method is showing that a proper SOAP XML response is being returned with results, so I know that it is making the call to the service and returning correctly. For some reason though I am left with a NULL ItemSearch object.</p>
  
  <p>The code for my classes are the following:</p>
  
  <pre><code>class AmazonSigningBehaviorExtensionElement : BehaviorExtensionElement
  {
      public AmazonSigningBehaviorExtensionElement()
      {
      }
  
      public override Type BehaviorType
      {
          get
          {
              return typeof(AmazonSigningEndpointBehavior);
          }
      }
  
      protected override object CreateBehavior()
      {
          return new AmazonSigningEndpointBehavior(AccessKeyId, SecretKey);
      }
  
      [ConfigurationProperty("accessKeyId", IsRequired = true)]
      public string AccessKeyId
      {
          get { return (string)base["accessKeyId"]; }
          set { base["accessKeyId"] = value; }
      }
  
      [ConfigurationProperty("secretKey", IsRequired = true)]
      public string SecretKey
      {
          get { return (string)base["secretKey"]; }
          set { base["secretKey"] = value; }
      }
  }
  
  
  public class AmazonSigningEndpointBehavior : IEndpointBehavior {
      private string  _accessKeyId    = "";
      private string  _secretKey  = "";
  
      public AmazonSigningEndpointBehavior()
      {
          this._accessKeyId = ConfigurationManager.AppSettings["accessKeyId"];
          this._secretKey = ConfigurationManager.AppSettings["secretKey"];
      }
  
      public AmazonSigningEndpointBehavior(string accessKeyId, string secretKey) {
          this._accessKeyId   = accessKeyId;
          this._secretKey     = secretKey;
      }
  
      public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime) {
          clientRuntime.MessageInspectors.Add(new AmazonSigningMessageInspector(_accessKeyId, _secretKey));
      }
  
      public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher) { return; }
      public void Validate(ServiceEndpoint serviceEndpoint) { return; }
      public void AddBindingParameters(ServiceEndpoint serviceEndpoint, BindingParameterCollection bindingParameters) { return; }
  }
  
  public class AmazonSigningMessageInspector : IClientMessageInspector {
      private string  _accessKeyId    = "";
      private string  _secretKey  = "";
  
      public AmazonSigningMessageInspector(string accessKeyId, string secretKey) {
          this._accessKeyId   = accessKeyId;
          this._secretKey     = secretKey;
      }
  
      public object BeforeSendRequest(ref Message request, IClientChannel channel) {
          // prepare the data to sign
          string      operation       = Regex.Match(request.Headers.Action, "[^/]+$").ToString();
          DateTime    now             = DateTime.UtcNow;
          string      timestamp       = now.ToString("yyyy-MM-ddTHH:mm:ssZ");
          string      signMe          = operation + timestamp;
          byte[]      bytesToSign     = Encoding.UTF8.GetBytes(signMe);
  
          // sign the data
          byte[]      secretKeyBytes  = Encoding.UTF8.GetBytes(_secretKey);
          HMAC        hmacSha256      = new HMACSHA256(secretKeyBytes);
          byte[]      hashBytes       = hmacSha256.ComputeHash(bytesToSign);
          string      signature       = Convert.ToBase64String(hashBytes);
  
          // add the signature information to the request headers
          request.Headers.Add(new AmazonHeader("AWSAccessKeyId", _accessKeyId));
          request.Headers.Add(new AmazonHeader("Timestamp", timestamp));
          request.Headers.Add(new AmazonHeader("Signature", signature));
  
          return null;
      }
  
      public void AfterReceiveReply(ref Message reply, object correlationState)
      {
  
  
      }
  }
  </code></pre>
  
  <p>I have seen this problem everywhere, but nobody has posted a fix for it anywhere. Somebody please help me with this.</p>