Web Applications

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:

a. Project Type: Visual Basic Projects

b. Templates: ASP.NET Web Application

c. Name: Chapter2WEB

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.

Figure 2.9. Layout for WebForm1.aspx.


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.

Listing 2.4. WebForm1 Page_Load Event Handler
Private Sub Page_Load(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load

    'Put user code to initialize the page here
    Dim svc As New Chapter1.FirstService()
    Dim aName As Chapter1.Name
    Dim authorNames() As Chapter1.Name
    Dim tr As TableRow
    Dim fName As TableCell
    Dim lName As TableCell

    Try
        ' Get the names of the authors
        authorNames = svc.GetAuthorNames()
        ' For every name in the array,
        ' create a new table row and two
        ' cells for that row.
        ' Set the text and add the row to
        ' the table.
        For Each aName In authorNames
            tr = New TableRow()
            fName = New TableCell()
            lName = New TableCell()
            fName.Text = aName.First
            lName.Text = aName.Last
            tr.Cells.Add(fName)
            tr.Cells.Add(lName)
            Me.authorTable.Rows.Add(tr)
        Next
    Catch ex As Exception
        Response.Write(ex.ToString())
    Finally
        ' Clean up after the service.
        svc.Dispose()
    End Try
End Sub
					

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.

Figure 2.10. Live view of WebForm1.aspx.


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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset