Web Services Calling Other Web Services

You might sometimes find it useful to have a Web Service you write call other Web Services. There are many reasons to have Web Services talk to each other. SOAP can be used as a cross-language and/or cross-platform communication mechanism. Your data center might be designed to use Web Services to improve the ability to upgrade or change hardware without significant changes to applications that talk to that hardware. You may also have partners or customers whose applications use your Web Services to coordinate activities, update data, and, in general, automate a large amount of work.

For a simple demonstration, we are going to write a Web Service that calls the FirstService.GetPerson Web Method. It will examine the data and return the number of days that person has been alive at the time the method was called.

The first thing we need to do is create another ASP.NET Web Service. You did this once already in Chapter 1. This time, name the project Chapter2WS. When the wizard is done creating everything for you, add a Web Reference to http://localhost/Chapter1/Chapter1.vsdisco and rename the namespace from localhost to Chapter1.

Finally, open up Service1.asmx.vb (the code view for Service1.asmx) and add a new Web Method named GetDaysAlive. This function should return a long value. Listing 2.5 shows the code.

Listing 2.5. GetDaysAlive Web Method
<WebMethod()> Public Function GetDaysAlive() As Long
    Dim svc As New Chapter1.FirstService()
    Dim aPerson As Chapter1.Person
    Dim ts As TimeSpan

    GetDaysAlive = -1
    Try
        aPerson = svc.GetPerson()

        ' Get the difference between now and the
        ' person's birthday.
        ts = DateTime.Now.Subtract(aPerson.birthDay)

        GetDaysAlive = ts.Days
    Catch ex As Exception
        ' Let the caller know something bad happened
        Dim aFault As New SoapException( _
            "Call to next Web Service failed", _
            SoapException.ClientFaultCode)
        ' Need to call Dispose since we are throwing again
        ' out of this catch block.
        svc.Dispose()
        Throw aFault
    End Try

    svc.Dispose()
End Function

We will let ASP.NET write the user interface for this one. Navigating to http://localhost/Chapter2WS/Service1.asmx?op=GetDaysAlive on our machine, we see that the HTTP/GET response and request pair will look pretty simple. By sending a request to http://localhost/Chapter2WS/Service1.asmx/GetDaysAlive?, we can expect to get back a single long value. Executing this code on September 3, 2001, I got the following response when clicking the Invoke button on the ASP.NET–generated Web page:

<?xml version="1.0" encoding="utf-8"?>
<long xmlns="http://tempuri.org/">10743</long>

To make sure this number looks right, we should look at the SOAP request/response pair sent by the Chapter2WS Web Service.

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>
    <GetPerson 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>
    <GetPersonResponse xmlns="http://tempuri.org/">
      <GetPersonResult>
        <theName>
          <First>Scott</First>
          <Middle>Christopher</Middle>
          <Last>Seely</Last>
        </theName>
        <birthDay>1972-04-05T00:00:00.0000000-07:00</birthDay>
      </GetPersonResult>
    </GetPersonResponse>
  </soap:Body>
</soap:Envelope>

Given that I am claiming to be approximately 29.5 years old, that would mean that the number returned should be something less than 365.25 * 29.5, which is equal to 10774.875. 10743 is about 32 days under that number. Given that, I would need that many days to actually be 29.5 years old, it looks like the Web Method is working correctly.

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

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