Article
Use Amazon Web Services in ASP.NET
Page: 1 2
Caching
As we discussed previously, Amazon services need to be cached. Thanks to .NET's simple-to-use caching engine, we can add caching abilities to our application in a few short lines.
First, we need to add the response of our search to the cache. This is held in the ItemSearchResponse response object. The key to accessing the result from the cache is given by the value stored within the text box -- the unique part of our search query. As per the license agreement requirements, this cache item will expire in 24 hours after we add it:
Cache.Insert(TextBox1.Text.ToLower(), response, null,
DateTime.Now.AddHours(24), TimeSpan.Zero);
At the start of our query, we can now check to see if we already have our item in the cache, using the TextBox1.Text as our key:
if (Cache[TextBox1.Text.ToLower()] == null)
If we haven't got a cached result for the query, we can use the code defined earlier to go grab it. Otherwise, we need to retrieve our ItemSearchResponse from the system cache:
response = (ItemSearchResponse)Cache[TextBox1.Text];
Our completed code looks like this, with caching enabled (new lines are bolded):
//have we this in the cache already?
ItemSearchResponse response;
if (Cache[TextBox1.Text] == null)
{
AWSECommerceService aws = new AWSECommerceService();
ItemSearchRequest request = new ItemSearchRequest();
request.SearchIndex = "Books";
request.Power = "title:" + TextBox1.Text;
request.ResponseGroup = new string[] { "Small" };
request.Sort = "salesrank";
ItemSearchRequest[] requests = new ItemSearchRequest[] { request };
ItemSearch itemSearch = new ItemSearch();
itemSearch.AssociateTag = "myassociatetag-20";
itemSearch.SubscriptionId = "XXXXXXXXXXXXXXXXX";
itemSearch.Request = requests;
response = aws.ItemSearch(itemSearch);
Cache.Insert(TextBox1.Text, response, null,
DateTime.Now.AddHours(24), TimeSpan.Zero);
}else
{
response = (ItemSearchResponse)Cache[TextBox1.Text];
}
Items info = response.Items[0];
Item[] items = info.Item;
Label1.Text = "";
for (int i = 0; i < items.Length; i++)
{
Item item = items[i];
Label1.Text += "Book Title: " + item.ItemAttributes.Title + "<br />";
}
}
Amazon Web Service to DataSet
It's now time to encapsulate this functionality so that we can reuse the service more easily. The DataSet type within .NET is perfect for displaying structured data on our Web forms.
Our code will be sectioned into two methods. The AmazonToDataSet method will produce a DataSet from an ItemSearchResponse object; grabAmazonSearch will encapsulate our code to retrieve the ItemSearchResponse from a query string:
public DataSet AmazonToDataSet(string PowerSearchString)
{
DataSet ds;
//have we this in the cache already?
if (Cache[PowerSearchString] == null)
{
//create our query
ItemSearchResponse response =
grabAmazonSearch(PowerSearchString);
//Construct the dataset to store the results
ds = new DataSet();
ds.Tables.Add();
ds.Tables[0].Columns.Add("ProductName");
ds.Tables[0].Columns.Add("AuthorName");
ds.Tables[0].Columns.Add("Price");
ds.Tables[0].Columns.Add("URL");
Items info = response.Items[0];
Item[] items = info.Item;
DataRow dr;
//iterate the results adding new rows with
//the values from the search result item.
for (int i = 0; i < items.Length; i++)
{
Item item = items[i];
dr = ds.Tables[0].NewRow();
dr["ProductName"] = item.ItemAttributes.Title;
dr["AuthorName"] = item.ItemAttributes.Author[0];
dr["Price"] = item.ItemAttributes.ListPrice;
dr["URL"] = item.DetailPageURL;
ds.Tables[0].Rows.Add(dr);
}
//add the dataset to the cache
Cache.Insert(PowerSearchString, ds, null,
DateTime.Now.AddHours(24), TimeSpan.Zero);
}
else
{
ds = (DataSet)Cache[PowerSearchString];
}
//return the dataset
return ds;
}
private ItemSearchResponse grabAmazonSearch(string PowerSearchString)
{
ItemSearchResponse response;
AWSECommerceService aws = new AWSECommerceService();
ItemSearchRequest request = new ItemSearchRequest();
request.SearchIndex = "Books";
request.Power = PowerSearchString;
request.ResponseGroup = new string[] { "Small" };
request.Sort = "salesrank";
ItemSearchRequest[] requests = new ItemSearchRequest[] { request };
ItemSearch itemSearch = new ItemSearch();
itemSearch.AssociateTag = "myassociatetag-20";
itemSearch.SubscriptionId = "XXXXXXXXXXXXXXX";
itemSearch.Request = requests;
response = aws.ItemSearch(itemSearch);
return response;
}
If we drop a DataGrid onto a Web form, we can fill it with the results from an Amazon query in just two lines of code:
Datagrid1.DataSource = AmazonToDataSet("title: " + TextBox1.Text);
Datagrid1.DataBind();
This provides the following output:

Summary
Using Amazon Web Services on your Website can create new revenue opportunities for your site, and provide fresh content for your users to view. Make the most of it!