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. namespace WcfDemo
  4. {
  5. [ServiceContract]
  6. public interface IStudent
  7. {
  8. [OperationContract]
  9. Student GetStudent();
  10. }
  11. [DataContract]
  12. public class Student
  13. {
  14. int Id = 0;
  15. string Name = "";
  16. string Mobile = "";
  17. [DataMember]
  18. public int StudentId
  19. {
  20. get { return Id; }
  21. set { Id= value; }
  22. }
  23. [DataMember]
  24. public string StudentName
  25. {
  26. get { return Name; }
  27. set { Name = value; }
  28. }
  29. [DataMember]
  30. public string MobileNo
  31. {
  32. get { return Mobile ; }
  33. set { Mobile = value; }
  34. }
  35. }
  36. }
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
Next >> KnownType Attribute in WCF: Part 6