Saturday, April 24, 2010

Contracts

WCF contracts are similar to our real life contracts that you and i sign. A contract i may sign could contain the type of work i perform and what information i make to available to others.
Data Contract:
Data Contract describes the data to be exchanged.
Example:
Let us consider two strangers on the street and wanting to start up discussion.How ever before these two strangers talking they need to agree on the "data" that they will be passing back and forth. This is data contract.

That is, to communicate, the client and the service do not have to share the same types, only the same data contracts.
The data contract specifically defines what each parameter and return type will be serialized / deserialized (Binary / XML) in order be exchanged from one party to the another.

WCF uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data (convert it to and from XML). All .NET Framework primitive types, such as integers and strings, as well as certain types treated as primitives, such as DateTime and XmlElement, can be serialized with no other preparation and are considered as having default data contracts.

New complex types that we create must have a data contract defined for them to be serializable.

Example
The following example shows a service contract (an interface) to which the ServiceContractAttribute and OperationContractAttribute attributes have been explicitly applied. The example shows that primitive types do not require a data contract, while a complex type does.


The following example shows how a data contract for the MyTypes.PurchaseOrder type is created by applying the DataContractAttribute and DataMemberAttribute attributes to the class and its members

No comments:

Post a Comment