What a DataContract and DataMember Are in WCF: Part 5

Before reading this article, I highly recommend reading my previous parts:
DataContact
 
A datacontract is a formal agreement between a client and service that abstractly describes the data to be exchanged. In WCF, the most common way of serialization is to make the type with the datacontract attribute and each member as datamember.
 
Creating a basic DataContract and DataMember
  1. using System.Runtime.Serialization;  
  2. using System.ServiceModel;  
  3.   
  4. namespace WcfDemo  
  5. {  
  6.     [ServiceContract]  
  7.     public interface IStudent  
  8.     {  
  9.         [OperationContract]  
  10.         Student GetStudent();  
  11.     }  
  12.       
  13.     [DataContract]  
  14.     public class Student  
  15.     {  
  16.         int Id = 0;  
  17.         string Name = "";  
  18.         string Mobile = "";  
  19.         [DataMember]  
  20.         public int StudentId  
  21.         {  
  22.             get { return Id; }  
  23.             set { Id= value; }  
  24.         }  
  25.   
  26.         [DataMember]  
  27.         public string StudentName  
  28.         {  
  29.             get { return Name; }  
  30.             set { Name = value; }  
  31.         }  
  32.   
  33.         [DataMember]  
  34.         public string MobileNo  
  35.         {  
  36.             get { return Mobile ; }  
  37.             set { Mobile = value; }  
  38.         }  
  39.     }  
  40. }  
In Service1.svc.cs
  1. namespace WcfDemo  
  2. {  
  3.     public class Service1 : IStudent  
  4.     {  
  5.         public Student GetStudent()  
  6.         {  
  7.             Student objStudent = new Student();  
  8.             objStudent.StudentId = 1;  
  9.             objStudent.StudentName = "Pramod";  
  10.             objStudent.MobileNo = "9876543210";  
  11.             return objStudent;  
  12.         }  
  13.     }  
  14. }  
Properties of DataMember
  • EmitDefaultValue: We can set a default value in the .Net Framework. We can set the default value in the datamember. We can do this using the EmitDefaultValue property. By default it is false.
    1. [DataMember(EmitDefaultValue=false)]  
    2. public int StudentId  
    3. {  
    4.    get { return Id; } set { Id = value; }  
    5. }  
  • IsRequired: By using this property we can set the datamemeber as mandatory. By default it is false.
    1. [DataMember(IsRequired=true)]  
    2. public int StudentId  
    3. {  
    4.    get { return Id; } set { Id = value; }  
    5. }  
  • Name: By using this property we can set a datamember name when the schema is generated. By default, it is what we declare in our datamember. In code I have written the datamember as in the following;
    1. [DataMember(Name=“RegistrationNo”)]  
    2. public int StudentId  
    3. {  
    4.    get { return Id; } set { Id = value; }  
    5. }  
    But now, I want to show RegistrationNo instead of StudentId in XML. In that case we use the Name property.

  • Order: By using this property we can set the datamemeber order. In other words, we set which datamember shows first or which shows last.
    1. [DataMember(Order=1)]  
    2. public int StudentId  
    3. {  
    4.    get { return Id; } set { Id = value; }  
    5. }  


Similar Articles