Before reading this article, please go through the following articles:
- Day 1 - WCF Introduction and Contracts
- Day 2: WCF Fault Contacts
- Day 3: WCF Message Exchange Patterns
DataContract
Data contracts are the conceptual agreement of the format and structure of the data in the message exchanged between a service and client. It is a format of data passed to and from the service operations. It defines the structure and types of data exchanged in service messages.
DataContractAttribute
The DataContractAttribute is defined in the System.Runtime.Serialization namespace. It is used to declare a type as a DataContract.
Properties of DataContractAttribute
- Name - It determines the type name as it is generated in the resulting schema.
- Namespace - It sets the target namespace of the schema.
DataMemberAttribute
The DataMemberAttribute is also the part of the System.Runtime.Serialization namespace. It is applied to the members and it is used to declare that the members should be included in the serialization.
Properties of DataMemberAttribute
- EmitDefaultValue - If you want to add default values in the serialization then set the EmitDefaultValue to true else EmitDefaultValue to false. By default it is true.
- IsRequired - It controls the minOccurs attribute for the schema element. The default value is false.
- Name - It creates the schema element name generated for the member.
- Order - It maintains the order of each element in the schema. By default it appears alphabetically.
Note
- For example, you set the EmitDefaultValue to false and IsRequired to true and you are not assigning any value, at that time it creates a problem. Because the serializer emits the default value and we are not passing any value, on the other side it is required. That is why it throws an error.
- If you apply a data member attribute to both property and field, then it generates duplicate members in the schema.

Technical LearningPosted Dec 23, 2014, 5:03 AM
Can u explain about the schema ?
Akshay PatelPosted Oct 3, 2012, 6:56 AM
@Gaurav - Technically, no. This is dependent on whether you use the DataContractSerializer or not. But you should use.
Swati AgarwalPosted Oct 3, 2012, 6:03 AM
nice explanation....
Gaurav ChauhanPosted Oct 3, 2012, 6:01 AM
very knowledgible... looking forward to see some more......
Gaurav GuptaPosted Oct 3, 2012, 6:01 AM
Is it neccessary to use DataContractAttribute when we can use DataContract without it?
Varesh TuliPosted Oct 3, 2012, 6:00 AM
Thanks for this article.. very knowledgeable...
lavisha rastogiPosted Oct 3, 2012, 5:57 AM
very nicely u have presented......thanxs
Deepak MiddhaPosted Oct 3, 2012, 5:45 AM
Very nice explanation......... Thanx for posting
Megha GoyalPosted Oct 3, 2012, 12:39 AM
Nice article...