DataContract Vs MessageContract in WCF

By | February 25, 2014

Data Contract in WCF is an agreement between parties (i.e. a service and a client) that describes what type of data will be exchanged between them? On the other hand, Message Contract describes the structure of SOAP messagethatis passed between parties (i.e. a service and a client). Using Data Contract, we actually control the contents (i.e. Payload data or message body) of a SOAP message while Message Contract provides complete control over structure of SOAP message.In Windows Communication Foundation, there are two types of Contracts:

  • Behavioral Contracts
  • Structural Contracts
Both DataContract and MessageContract are structural contracts that compliment each other serving different purposes. For more detailed understanding of contracts in WCF, follow my WCF FAQs tutorial – Part 1. Lets now understand the difference between DataContract and MessageContract with the help of an example.

DataContract Example:

Consider a WCF Service having an operation as follows:
public interface IStudentService
        StudentInfo GetStudentById(int studentId);
Here the return type is “StudentInfo” which is basically a CLR type defined below as a DataContract.
public class StudentInfo
[DataMember(Name=”StudentId“, Order=1)]
public int ID;
       [DataMember(Name=”FirstName“, Order=2)]
public string FName;

[DataMember(Name=”LastName, Order=3)]
public string LName;
When the service method will be called, this StudentInfo type will be returned as part of the SOAP response message body.
If we look into this “StudentInfo” Data Contract, we are providing different details about data being transferred back to consumer. For example,
  1. Order = 1,2,3 defines the order of the data member in response message body.
  2. Name = “some name” dictates the name of particular data member as part of response message body.
But through all these attributes, we are actually controlling the contents of the SOAP message body only. And in most of the cases, developers are concerned with controlling the body part of the message instead of whole SOAP message.

MessageContract Example:

As we have seen in this WCF Tutorial in above example that DataContract has limited control over the SOAP message and all that control is related to contents inside body of the SOAP message. But there are scenarios, when we need more control over SOAP message. So, MessageContract is the answer in such cases.

Now consider a business scenario, an authentication code is required to access our service i.e. IStudentService. In this case functionality provided by our service will remain the same but authentication code validity is additional pre-requisite now. So in such cases, SOAP message header is the most reasonable place to store the authentication code while calling the service method.

Look into the below implementation of this scenario:

[MessageContract(IsWrapped = false)]
public class StudentInfoRequestMessage
       public string AuthCode;

      public int StudentId;
[MessageContract(IsWrapped = false)]
public class StudentInfoResponseMessage
      public StudentInfo MyStudentInfo;
public interface IStudentService 
        StudentInfoResponseMessage GetStudentById(StudentInfoRequestMessage stdInfoMessage); 
public class StudentService : IStudentService
     public StudentInfoResponseMessage GetStudentById(StudentInfoRequestMessage stdInfoMessage)
private const string AuthCode = “ali123salman”;

               //Validation Check
               if (stdInfoMessage.AuthCode != AuthCode)
                       //fault response
              //routine code

Hopefully, now reader will have good understanding about the difference between DataContract and MessageContract in WCF.

Top 10 Interview Questions and Answers Series:

Category: Uncategorized Tags: , ,


Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at,, and

3 thoughts on “DataContract Vs MessageContract in WCF

  1. Rakesh Chaubey

    Really good article ..thanks a lot man … and keep up the good work..

  2. Pingback: 5 simple steps to create your first RESTful service | WCF Tutorial

Comments are closed.