Data Contracts in WCF Service

Data Contracts

A Data Contract defines what data type to be passed to or from the client. In the WCF service, the Data Contract takes a major role for serialization and deserialization. There are two types of Data Contracts.
 
Data Contract

This contract declares and defines the class to be serialized for the client to access. If the Data Contract tag is not used then the class will not be serialized or deserialized.
  1. [DataContract]  
  2. public class Customer  
  3. {  
  4. }  
Data Member

This declares and defines properties inside a class, the property that doesn't use a Data Member tag will not be serialized or deserialized.

Example
  1. [DataContract]  
  2. public class Customer  
  3. {  
  4.    [DataMember]  
  5.    public int ID { getset; }  
  6.    [DataMember]  
  7.    public string Name { getset; }  
  8.    public string ContactNo { getset; }  
  9. }   
In the preceding example the ContactNo property will not be given access to the client because it will not be serialized or deserialized though it is not used as a Data Member attribute.

Summary

Hope you must have learned about DataContract.
Ebook Download
View all
Learn
View all