This article and the associated Windows Communication Foundation (WCF) sample (download) demonstrate how to implement and configure a WCF client using a custom message inspector to sign SOAP based Amazon Product Advertising API requests. The sample uses the .NET Framework 4.0 to communicate with Amazon’s Product Advertising API. The Product Advertising API allows you to:
- Access Amazon Product Selection: The Product Advertising API gives you access to Amazon’s selection of millions of products in categories such as books, music, digital downloads and electronics from Amazon.com, Amazon.ca, Amazon.co.uk, Amazon.de, Amazon.fr, and Amazon.co.jp.
- Leverage Amazon Product Discovery capabilities: The Product Advertising API lets you leverage Amazon’s customer-centric features such as Product Search, Customer Reviews, Similar Products, Accessories, Listmania Lists and more.
- Monetize your website: The Product Advertising API allows you to include your associate tag in API requests to automatically format the URLs returned by the API to ensure that you earn referral fees when users you refer to Amazon sites buy qualifying products.
This sample makes use of a WCF custom MessageInspector as described in the MSDN documentation. A message inspector is an extensibility object that can be used in the service model's client runtime and dispatch runtime programmatically or through configuration and that can inspect and alter messages after they are received or before they are sent. The sample here is derived from Oren Trutner's article “Signing Amazon Product Advertising API – C#/WCF.” Oren’s example is extended to allow all the configuration to occur in the application configuration (app.config) file. All of the Amazon helper methods have been moved to a separate library that can be used/referenced in any project. A separate console app has been included that demonstrates how to use the app.config file to configure the WCF endpoints and how to add the custom MessageInspector behavior to the endpoint.
The sample is self-contained, and does not require you to read the entire post to use. Simply download the source, modify the app.config with your Amazon Product Advertising API keys, build and run.
The project includes the Amazon.PAAPI.WCF library of helper functions that can be used in any project to sign SOAP based Amazon Product Advertising API requests. The AmazonSigningMessageInspector class implements IClientMessageInspector and does the work of signing the SOAP request elements IAW Amazon’s specifications. The AmazonSigningEndpointBehavior class implements IEndpointBehavior to add the custom MessageInspector to the endpoint behavior. Finally, the AmazonSigningBehaviorExtensionElement class implements BehaviorExtensionElement so that the endpoint behavior can be configured in the app.config file of the executing program.
To use the library, create an application like the AmazonSOAP project included in the example solution. Add a reference to the Amazon.PAAPI.WCF library (copy local set to “true”). Modify the app.config file:
First, in the system.serviceModel section, add a child section “extensions” and then add the custom behaviorExtension –
<add name="signingBehavior" type="Amazon.PAAPI.WCF.AmazonSigningBehaviorExtensionElement, Amazon.PAAPI.WCF, Version=184.108.40.206, Culture=neutral, PublicKeyToken=null"/>
Next, add an endpointBehavior in the behaviors section that makes use of the behaviorExtension just defined:
<signingBehavior accessKeyId="yourAmazonPublicKey" secretKey="yourAmazonSecretKey" />
Finally, modify the client endpoint to use the endpointBehavior by adding the behaviorConfiguration attribute - behaviorConfiguration="amazonEndpointBehavior"
binding="basicHttpBinding" bindingConfiguration="AWSECommerceServiceBindingTransport" behaviorConfiguration="amazonEndpointBehavior"
contract="Amazon.PAAPI.AWSECommerceServicePortType" name="AWSECommerceServicePort" />
Hope this helps – good luck!
amazonProductAdvertisingAPI-SOAP-WCF.zip (1.11 mb)