Building a Web Services Conduit (Continued)
Creating the proxy to hit the Amazon.com Web service proved to be a fairly simple task. Initially, I used Visual Studio .Net's proxy generation wizard to create the proxy by loading a local WSDL file that I obtained from the Web service toolkit. You can also hit the file remotely at Amazon.com's site (http://soap.amazon.com/schemas2/AmazonWebServices.wsdl). However, the proxy class was not generated successfully on the first attempt. By looking at the WSDL I discovered that it referenced 1999 in the schema namespace URI, which caused Visual Studio .Net's proxy generation process to fail because 1999 isn't part of the WSDL 1.1 specification (locate the latest WSDL specification information regarding the namespace URI here: www.w3.org/TR/wsdl12/#eii-types). However, by changing each of the 1999 references to 2001 in my local copy of the WSDL, the proxy was created automatically.
If you don't have Visual Studio .Net, you can use the WSDL.exe command-line utility or the new ASP.Net Web Matrix editor (did I mention it's free?) from the ASP.Net Web Matrix site (www.asp.net), which also has a proxy generation wizard built in. You can see an example of creating a proxy in Visual Studio .Net (see Figure 1) and the proxy generation wizard in ASP.Net Web Matrix (see Figure 2).
After generating and compiling the proxy, I found that it could be used to hit a large number of Web service methods from the Amazon.com Web service (see Table 1). Looking through these methods, you can see that you can perform several different types of searches ranging from actors to authors to UPC numbers. I decided to work with the AsinSearchRequest() and AuthorSearchRequest() methods specifically because I wanted to provide .Net book information on the XML for ASP.NET Developers Web site. Both methods take an AsinRequest and AuthorRequest object, respectively, as a parameter.
Key to the Service
Within the User Control, private methods were added to generate both of these objects. Both the GenerateAsinRequest() method (see Listing 1) and the GenerateAuthorRequest() method (see Listing 2) show many of the AsinRequest and AuthorRequest properties. Before trying to hit the service, it's important that you get a developer key (the key is free) from Amazon.com.
After creating the methods that create the AsinRequest and AuthorRequest objects, the proxy must be instantiated and used to call the appropriate Web service method at the Amazon.com Web service. Because the User Control allows a search to be performed using the ASIN (also known as the ISBN number) or Author name, it first checks to see what type of data was passed on the QueryString and assigns the data to a local variable:
//Book's ASIN/ISBN number or author's
//name must be passed on the
//QueryString. If this data is not
//found, the asinNumber or
//authorName variables are assigned
//a value of empty strings
//(String.Empty)
string asinNumber = (Request[
"asin"]==null)?String.Empty:
Request["asin"];
string authorName = (Request[
"authorName"]==null)?String.Empty:
Request["authorName"];
//If the User Control can't find
//either the ASIN number or author
//name then stop processing the page
if (asinNumber == String.Empty &&
authorName == String.Empty) {
Response.Write(
"Please supply either the
author " +
"name or book ASIN.");
Response.End();
}
Assuming that the data to perform the Web service search is found, the proxy object (named AmazonSearchRequest) is invoked, and the proper Web service method is called based on what data was passed to the User Control. You can see how to create the proxy, create the proper ASIN or author request object, and then call the appropriate Web service method (see Listing 3). Each Web service method will return an object named ProductInfo.
Back to top
|