Data Contract in WCF

A data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged.
  1. Data contract can be explicit or implicit. Simple type such as int, string etc has an implicit data contract. User defined object are explicit or Complex type, for which you have to define a Data contract using [DataContract] and [DataMember] attribute.
     
  2. WCF uses a serialization engine called the Data Contract Serializer by default to serialize and deserialize data. We can also explicitly create a data contract by using DataContractAttribute and DataMemberAttribute attributes. This attribute can be applied to classes, structures, and enumerations.
  3. The DataMemberAttribute attribute must then be applied to each member of the data contract type to indicate that it is a data member, that is, it should be serialized.
     
  4. Data Contracts can be defined as follows:
    1. It describes the external format of data ed to and from service operations 
    2. It defines the structure and types of data exchanged in service messages
    3. It maps a CLR type to an XML Schema 
    4. It defines how data types are serialized and deserialized. Through serialization, you convert an object into a sequence of bytes that can be transmitted over a network. Through deserialization, you reassemble an object from a sequence of bytes that you receive from a calling application.
    5. It is a versioning system that allows you to manage changes to structured data.
       
  5. Namespace for holding DataContract and DataMember attributes.
System.Runtime.Serialization

Example
  1. Create User defined data type called Customer. This data type should be identified for serialization and deserialization by mentioning with [DataContract] and [DataMember] attribute.
  2. Add Wcf Service at DataContractinWCF.Web(Silverlight Web Project),We get IService1.cs and Service1.svc files.
     
  3. IService1.cs
    1. namespace DataContractinWCF.Web {  
    2.   [ServiceContract]  
    3.   public interface IService1 {  
    4.    [OperationContract]  
    5.    List < Customer > GetCustomerData(int CustomerID);  
    6.   }  
    7.   [DataContract]  
    8.   public class Customer {  
    9.    private string m_Name;  
    10.    private int m_Age;  
    11.    private int m_Salary;  
    12.    private string m_Designation;  
    13.    private string m_Manager;  
    14.    [DataMember]  
    15.    public string Name {  
    16.     get {  
    17.      return m_Name;  
    18.     }  
    19.     set {  
    20.      m_Name = value;  
    21.     }  
    22.   
    23.    }  
    24.   
    25.    [DataMember]  
    26.    public int Age {  
    27.     get {  
    28.      return m_Age;  
    29.     }  
    30.     set {  
    31.      m_Age = value;  
    32.     }  
    33.    }  
    34.   
    35.    [DataMember]  
    36.    public int Salary {  
    37.     get {  
    38.      return m_Salary;  
    39.     }  
    40.     set {  
    41.      m_Salary = value;  
    42.     }  
    43.    }  
    44.    [DataMember]  
    45.    public string Designation {  
    46.     get {  
    47.      return m_Designation;  
    48.     }  
    49.     set {  
    50.      m_Designation = value;  
    51.     }  
    52.    }  
    53.    [DataMember]  
    54.    public string Manager {  
    55.     get {  
    56.      return m_Manager;  
    57.     }  
    58.     set {  
    59.      m_Manager = value;  
    60.     }  
    61.    }  
    62.   }  
    In GetCustomerData method we have created the Customer instance and return to the client.

    As we have created the data contract for the Employee class, client will aware of this instance whenever he creates proxy for the service. 
  4. Service1.svc.cs :
    1. namespace DataContractinWCF.Web  
    2. {  
    3.  public classService1: IService1  
    4.  {  
    5.   public Cusomer GetCustomerData(int empId) {  
    6.    Cusomer CusDetail = new Cusomer();  
    7.    //Do something to get Customer details and assign to CusDetail properties  
    8.    return CusDetail;  
    9.   }  
    10.  }  
    11. }
  5. Client Side

    On client side we can create the proxy for the service and make use of it. The client side code is shown below.

    Build Web project and then Add Service reference to client side. Now we can access the service methods at client side.

    On button click event access the service as follows :
    1. Public void btnGetDetails_Click(object sender, EventArgs e) {  
    2.  ServiceReference1.Service1Client service = new ServiceReference1.Service1Client();  
    3.   
    4.  Cusomer CusDetails;  
    5.  CusDetails = service.GetCustomerData(CusomerId);  
    6.  //Do something on Customer details  
    7. }


Similar Articles