You should be seeing a pattern by now. No matter the medium used to access a Web Service, the actions you perform to call the Web Service do not change. The only thing that changes is the way you get your input or display the results.
For this particular example, we will get the list of authors of this book and display the names in a table. Once again, we will create a new project.
1. |
Select File, Project, New Project. |
2. |
Select the following items in the New Project dialog:
|
At this point, you should be looking at the design view of WebForm1.aspx. On this screen, we will place a simple caption and a table. Set up the table to have one row and two columns. That row has a heading. The code will grow the table as appropriate. Figure 2.9 shows one way to display this.
After the Web form is configured, you need to add a Web reference to http://localhost/Chapter1/Chapter1.vsdisco and rename the namespace from localhost to Chapter1. With the proxy in place, we only need to add the author names to the table. To do this, open up the Solution Explorer pane and right-click WebForm1.aspx. Select View Code from the pop-up menu. We are now going to edit the Page_Load event handler to call to the FirstService.GetAuthorNames Web Method and add the author names to the table. Listing 2.4 shows the code.
Again, you see nothing terribly fancy here. Perhaps the most interesting bit of code here is that this uses the actual compiled source code instead of script to create the Web page. Depending on the number of items returned by the GetAuthorNames call, the code adds an appropriate number of rows to the table. This allows for some flexibility when filling in the table. If an author joins or leaves the authoring team, this code does not need to be modified. The correct number of rows will always be present. When viewing the page, we get a list of all four authors, just as expected. Figure 2.10 shows the results.
We will finish up this section with a quick look at the SOAP request and response. You may want to compare the results here against those in Chapter 1, which was done using HTTP/GET instead of SOAP.
Request:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetAuthorNames xmlns="http://tempuri.org/" /> </soap:Body> </soap:Envelope>
Response:
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <soap:Body> <GetAuthorNamesResponse xmlns="http://tempuri.org/"> <GetAuthorNamesResult> <Name> <First>Scott</First> <Middle /> <Last>Seely</Last> </Name> <Name> <First>Michael</First> <Middle /> <Last>Carnell</Last> </Name> <Name> <First>Jeffrey</First> <Middle /> <Last>Huntsman</Last> </Name> <Name> <First>Deon</First> <Middle /> <Last>Schaffer</Last> </Name> </GetAuthorNamesResult> </GetAuthorNamesResponse> </soap:Body> </soap:Envelope>
We will finish this chapter. by looking at a Web Service that uses another Web Service.