Passing Complex Types

ASP.NET makes it exceptionally easy to pass complex types as Web Service arguments and return values. If the complex type is passed as a method argument, ASP.NET will only be able to support SOAP messaging. This is because no definition exists to describe how to pass complex types as arguments via HTTP/GET or HTTP/POST. You can return complex types via these methods. So that we can see the immediate results of our coding via Internet Explorer, we will return the type.

Many people who learn of this functionality are often surprised to find out that when a value is passed via a Web Service, only the publicly visible fields are serialized. The receiver of the object has no access to any methods that the object may expose. Instead, the recipient of the object must already know what to do with the object when it is received. If you need to pass objects with methods intact, do not use Web Services. The Common Language Runtime (CLR) within .NET provides mechanisms when object identity and object value must be preserved. Web Services can only preserve the public object values.

To keep things simple, we will create a class called Person. Person will contain a Name object and a date for the person's birthday. Listing 1.2 shows a pair of classes that form a complex type. Name contains three strings and is itself a complex type. Person makes it a little more interesting for serialization because it must also serialize a Name inside.

Listing 1.2. Source for Name and Person Objects
Public Class Name
    Public First As String
    Public Middle As String
    Public Last As String
End Class

Public Class Person
    Public theName As Name
    Public birthDay As Date

    Public Sub New()
        theName = New Name()
    End Sub
End Class
					

So far, so good. Now, we need to add a Web Method to the FirstExample object that returns a Person, as shown in the following code:

<WebMethod()> Public Function GetPerson() As Person
    GetPerson = New Person()
    GetPerson.birthDay = System.DateTime.Parse("April 5, 1972")
    GetPerson.theName.First = "Scott"
    GetPerson.theName.Middle = "Christopher"
    GetPerson.theName.Last = "Seely"
End Function

To test things out, we navigate over to http://localhost/Chapter1/FirstService.asmx and click GetPerson. Here, we see that the request and response via Internet Explorer will be fairly plain:

Request:

GET /Chapter1/FirstService.asmx/GetPerson? HTTP/1.1
Host: localhost

Response:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<Person xmlns="http://tempuri.org/">
  <theName>
    <First>string</First>
    <Middle>string</Middle>
    <Last>string</Last>
  </theName>
  <birthDay>dateTime</birthDay>
</Person>

When we invoke the method, the Web Service responds as expected with the following:

<?xml version="1.0" encoding="utf-8"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns="http://tempuri.org/">
  <theName>
    <First>Scott</First>
    <Middle>Christopher</Middle>
    <Last>Seely</Last>
  </theName>
  <birthDay>1972-04-05T00:00:00.0000000-07:00</birthDay>
</Person>

As you can see, the Person element came across with two sub-elements—theName and birthDay. The amazing part here is that no object-specific code had to be written to write it out as XML.

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

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