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.
Sample Code
- Create one DataContract for Place. Add the data members PlaceCode and PlaceName.
- Set the order for the data members and set IsRequired to true for PlaceCode.
- Create one OperationContract with return type Place.
- namespace DataContractServiceApp
- {
- [ServiceContract]
- public interface IService1
- {
- [OperationContract]
- Place GetPlace(string PlaceCode);
- }
- [DataContract(Name ="Place", Namespace ="")]
- public classPlace
- {
- [DataMember(Name ="PlaceCode", Order = 1, IsRequired=true)]
- public string PlaceCode;
- [DataMember(Name ="PlaceName", Order = 2)]
- public string PlaceName;
- }
- }
- In the service class, the GetPlace method is implemented and in that we create an object of place, assign some values to it and return a Place object.
- namespace DataContractServiceApp
- {
- public class Service1 : IService1
- {
- public Place GetPlace(string PlaceCode)
- {
- Place place = new Place();
- // Get placename by placecode from database.
- return place;
- }
- }
- }


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