Contract = Agreement,

Data Contract = Data Agreement,

Data Agreement between whom? The agreement between the client and the server.

What exactly does the agreement contain? The structured data to be exchanged between the client and the server.

I hope you guys understand the preceding lines, yes a Data Contract is an agreement between the client and server about the structure of data being exchanged. A Data Contract describes the external format, structure and type of data being exchanged. It also defines how the data types are serialized and deserialized. The serialization happens on the client side to convert the objects into the messages that can be streamed over the network. On the other hand the dispatcher in the server side performs deserialization to convert the objects into data types and pass it to the service methods for execution. To understand the various stages of data transfer from a client to a server and how the parameters are converted into objects and the objects converted into messages, kindly refer to my previous article “WCF Run Time Architecture”.


WCF uses the DataContractSerializer class from the namespace System.RunTime.Serialization to serialize and deserialize the objects. This serializer is specially developed for WCF to perform quickly, effectively and powerfully.

DataContractSerializer supports the following data types:

  • Primitive data types
  • Data types with the [DataContract] attribute
  • Class marked as Serializable
  • Class that implements the IXmlSerializable
  • Enumerations, collections and generic collections

Primitive data types are already serializable, so no serialization measures are required for the primitive data types. Apart from the primitive types, whatever the complex types are used as parameters, return values in the service, we need to treat the class with the [DataContract] Attribute and the properties and member variables with the [DataMember] attribute. These attributes are actually used by the DataContractSerializer class to control the serialization/deserialization. In other words, these two attributes tell the Datacontractserializer how to serialize the complex types. [DataContract] is an opt-in model which means that only the member variables or properties that are marked with [DataMember] will be taken for the Serialization/Deserialization. The definition of the data is incorporated into the WSDL document in an XSD format.

Source Code Explanation


Look at the code snippet above, in the service interface we have exposed three methods called IsStockAvailable, CalculatePrice and GetWholeDealerInfo. Notice that the return type of the GetWholeSaleDealerInfo method is WholeSaleDealersInfo type, which is a complex type. We need to tell the DataContractSerializer about the complex type WholeSaleDealersInfo to serialize/deserialize.


Look at the code snippet above, the class has been decorated with the attribute [DataContract] and the properties as [DataMember]. This decoration is to tell the DataContractSerializer how and in which form its objects are to be converted.

Parameter Order

There is an attribute called Order in the Datamember where you can define the order of serialization and deserialization. Sometimes a datacontract should be serialized and deserialized in a specific order of data members.

By default the data members are serialized and deserialized in the following order:

  1. If the DataContract has been derived from the base DataContract then the data members of the base class will be processed first in alphabetical order
  2. DataMembers with no order property set will be processed in alphabetical order
  3. The remaining DataMembers will be processed in ascending order of the Order property value

Parameter Name

Using the DataMember Attribute, we can define the name of a DataMember on each property. It will be helpful to solve the DataContract versioning issues. The following source code will provide you a better understanding of this attribute.


Parameter IsRequired, EmitDefaultValue

By Default the IsRequired field will be false, if it is true then that specific property must be available during the serialization or deserialization process. If the IsRequired field is true and the EmitDefaultValue is true and if the value doesn’t provide the default value then it will be taken during the serialization. If the IsRequired field is true and the EmitDefaultValue is false and if the value doesn’t provide the default value then it will not be taken during the serialization and the serialization exception will be thrown.


Refer the below sample source code for further details

Source Code