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.
- [DataContract]
- public class Customer
- {
- }
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
- [DataContract]
- public class Customer
- {
- [DataMember]
- public int ID { get; set; }
- [DataMember]
- public string Name { get; set; }
- public string ContactNo { get; set; }
- }
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.