Working with Data Contracts

In the preceding sample WCF service, the data contract depended upon simple types or primitive data types. A .NET type of Integer was exposed, which in turn was mapped to an XSD type of int. You might not have noticed the input and output types actually defined in the WSDL document that was provided via the WCF-generated one, but they are there. These types are exposed through an imported .xsd document (a dynamic document). This bit of the WSDL document is presented here:

<wsdl:types>
   <xsd:schema targetNamespace="http://tempuri.org/Imports">
   <xsd:import schemaLocation="http://localhost:8000/calculator?xsd=xsd0"
    namespace="http://tempuri.org/" />
   <xsd:import schemaLocation="http://localhost:8000/calculator?xsd=xsd1"
    namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
  </xsd:schema>
</wsdl:types>

Typing in the XSD location of http://localhost:8000/calculator?xsd=xsd0 gives you the input and output parameters of the service. For instance, looking at the definition of the Add method, you will see the following bit of XML:

<xs:element name="Add">
   <xs:complexType>
      <xs:sequence>
         <xs:element minOccurs="0" name="a" type="xs:int" />
         <xs:element minOccurs="0" name="b" type="xs:int" />
      </xs:sequence>
   </xs:complexType>
</xs:element>
<xs:element name="AddResponse">
   <xs:complexType>
      <xs:sequence>
         <xs:element minOccurs="0" name="AddResult" type="xs:int" />
      </xs:sequence>
   </xs:complexType>
</xs:element>

This XML code indicates that there are two required input parameters (a and b) that are of type int; in return, the consumer gets an element called <AddResult>, which contains a value of type int.

As a builder of this WCF service, you didn't have to build the data contract, mainly because this service uses simple types. When using complex types, you have to create a data contract in addition to your service contract.

For an example of working with data contracts, you can create a new WCF service called ProVB_WCFWithDataContract (you will again host the services with a Console Application). As with the other samples this solution is available as part of the online code download. In this case, you will need to define a class that implements the data contract.

Like the service contract, which makes use of the <ServiceContract()> and the <OperationContract()> attributes, the data contract uses the <DataContract()> and <DataMember()> attributes.

The full WCF interface definition located in IHelloCustomer.vb is shown in Listing 11.7:

Listing 11.7 : Contract Definitions—IHelloCustomer.vb

<ServiceContract()> _
Public Interface IHelloCustomer
    <OperationContract()> _
    Function HelloFirstName(ByVal cust As Customer) As String
    <OperationContract()> _
    Function HelloFullName(ByVal cust As Customer) As String
End Interface
        
<DataContract()> _
Public Class Customer
    <DataMember()> _
    Public FirstName As String
    <DataMember()> _
    Public LastName As String
End Class

Similarly, the project contains the file HelloCustomer.vb, which contains the implementation class as shown in Listing 11.8:

Listing 11.8 : Service Implementation—HelloCustomer.vb

Public Class HelloCustomer
    Implements IHelloCustomer
    Public Function HelloFirstName(ByVal cust As Customer) As String _
      Implements IHelloCustomer.HelloFirstName
        Return "Hello " & cust.FirstName
    End Function
    Public Function HelloFullName(ByVal cust As Customer) As String _
      Implements IHelloCustomer.HelloFullName
        Return "Hello " & cust.FirstName & " " & cust.LastName
    End Function
End Class

This class, the Customer class, has two members: FirstName and LastName. Both of these properties are of type String. You specify a class as a data contract as part of the interface definition through the use of the <DataContract()> attribute:

<DataContract()> _
Public Class Customer
   ' Code removed for clarity
End Class

Now, any of the properties contained in the class are also part of the data contract through the use of the <DataMember()> attribute:

<DataContract()> _
Public Class Customer
    <DataMember()> _
    Public FirstName As String
    <DataMember()> _
    Public LastName As String
End Class

Finally, the Customer object is used in the interface, as well as the class that implements the IHelloCustomer interface.

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

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