Learning WCF Chapter2 Data Contracts

Source: Internet
Author: User
Tags net serialization wsdl


A Data Contract describes how the CLR types map to XSD schema definitions.
Data contracts is the preferred-to-enable serialization of complex types included in operation signatures as Paramete RS or return values.
You create a data contract by applying the DataContractAttribute to a type.
To include serialization,you decorate them with the datamemberattribute;
This was an opt-in process and that had nothing to do with visibility of members (Public,protected,private).
By Default,serialization are handled by the Datacontractserializer,a new serializer for WCF that succeeds the XmlSerializer Used for earlier technologies such as ASMX.

In this section,you would become acquainted with the key features of data contracts and common serialization Practices, Inc. Luding the following:
How to apply data contract attributes to exercise control over type serialization
version tolerance and data contract versioning techniques
implementing IExtensibleDataObject to support version tolerance
how to work with polymorphic types in the service contract
how enumerations, arrays, and collections are serialized

First you'll complete a lab this illustrates many of these scenarios,and then I ll explain the related attributes and feat Ures in greater detail.

Lab:working with Data contracts

For this lab, you'll modify a preexisting type,turning it into a data contract so that it can is included in the service Contract.
Using the DataContractAttribute and the Datamemberattribute,you would control type serialization through the Datacontractse Rializer.
You'll also test data contract version tolerance and implement the Iextensibledata interface in support of versioning.

Creating a data contract
In this part of the Lab,you ' ll turn a complex type to a data contract so that it can is included in a service contract,
And then take a look at the XSD schema produced for the type in the WSDL document.


1. Open the startup solution for this lab: <yourlearningwcfpath>\labs\chapter2\datacontracts\datacontracts.sln.
This solution includes the following projects:
ContentTypes
A class library that contains a linkitem type used by the service.
Linkitem is a custom type the holds a title,description,url and other details the can be associated with events,articles , Photos,files,and so forth.
In this example, Linkitem was used to the hold information about a gig (or event) for a band.
Gigmanager
A class library containing the service contract and service type.
The service contract exposes operations to save a Linkitem,and retrieve the saved Linkitem.
This example uses session to save the item.
See Chapter 5 for more on sessions.
Host
A Console Application for hosting the service.
Gigentry
A Windows client application that presents an interface for users to create and save a gig entry and retrieve the saved EN Try.

2. Try to run the solution at first.
From Visual studio,press F5 to run the Host and the Gigentry client application.
An invaliddatacontractexception would be thrown if the ServiceHost attempts to initialize. This place is nonsense, the default does not throw an exception, although I use. NET 3.0, it does not throw an exception
The error message indicates that the type Contenttypes.linkitem cannot is serialized.

  " gigmanagerservicecontract " " http://www.thatindigogirl.com/samples/2006/06 ", SessionMode = sessionmode.required)]    publicinterface  Igigmanagerservice    {        [OperationContract]        void  savegig (linkitem item);        [OperationContract]        Linkitem getgig ();    }

3. Review the service contract for the solution.
Go to the Gigmanager project and open GigManagerService.cs.
The service contract is shown in Example 2-13.
The Contenttypes.linkitem type is included in both operation signatures.

2. Try to run the solution at first.
From Visual studio,press F5 to run the Host and the Gigentry client application.
An invaliddatacontractexception would be thrown if the ServiceHost attempts to initialize.
The error message indicates that the type Contenttypes.linkitem cannot is serialized.

3. Review the service contract for the solution.
Go to the Gigmanager project and open GigManagerService.cs.
The service contract is shown in Example 2-13.
The Contenttypes.linkitem type is included in both operation signatures.

Example 2-13. Igigmanagerservice Service Contract
[ServiceContract (Name = "Gigmanagerservicecontract",
Namespace = "Http://www.thatindigogirl.com/samples/2006/06",
SessionMode = sessionmode.required)]
public interface Igigmanagerservice
{
[OperationContract]
void Savegig (Linkitem item);
[OperationContract]
Linkitem getgig ();
}

4. You is going to modify the Linkitem type to make it a valid data contract the can is included in the service contract .
Go to the ContentTypes project and add a reference to the System.Runtime.Serialization assembly.
Next,open LinkItem.cs and apply the DataContractAttribute to the class definition,
and apply the DataMemberAttribute to all private field so that they is included in the serialized type definition.
ADD a using statement for System.Runtime.Serialization as well.
The resulting type should appear as shown in Example 2-14.

5. Compile the solution and attempt to run the host once again.
This time won ' t see the exception because Linkitem is now a data contract.

6. View The service description in the browser;
You want to see the XSD schema representation of the Linkitem data contract.
Browse to the following address:http://localhost:8000 and click the "WSDL link to navigate to the service description.
This'll generate the service description so, can browse to the following address:http://localhost:8000/?xsd=xsd 2.
You should see a schema like the one shown in Example 2-15.

<xs:schema xmlns:xs= "Http://www.w3.org/2001/XMLSchema" xmlns:tns= "http://schemas.datacontract.org/2004/07/ ContentTypes "elementformdefault=" qualified " targetnamespace=" http://schemas.datacontract.org/2004/07/ ContentTypes ">
<xs:complextype name= "Linkitem" >
<xs:sequence>
<xs:element minoccurs= "0" name= "dateend" type= "Xs:datetime"/>
<xs:element minoccurs= "0" name= "DateStart" type= "Xs:datetime"/>
<xs:element minoccurs= "0" name= "Description" nillable= "true" type= "xs:string"/>
<xs:element minoccurs= "0" name= "ID" type= "Xs:long"/>
<xs:element minoccurs= "0" name= "Title" nillable= "true" type= "xs:string"/>
<xs:element minoccurs= "0" name= "URL" nillable= "true" type= "xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name= "Linkitem" nillable= "true" type= "Tns:linkitem"/>
</xs:schema>

Notice that the naming convention for each data member matches the field name in the type definition.
In addition,the order of all element in the schema sequence is alphabetical,
As opposed to the order of appearance in the type definition.
Another thing to note are that the namespace for the type does not match the service contract target namespace;
Instead,it uses the domain shemas.datacontract.org.

Note:

Other. NET serialization techniques is dependent on the reflection order of types.
This can introduce problems when developers inadvertently reorder the class Definition,not realizing it can cause Incompat Ibilities.

Learning WCF Chapter2 Data Contracts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.