Click here to Skip to main content
Click here to Skip to main content

DataContract Vs MessageContract in WCF

By , 26 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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 message that is 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:
[ServiceContract()]
public interface IStudentService
{
    [OperationContract()]
    StudentInfo GetStudentById(int studentId);
}
Here the return type is StudentInfo which is basically a CLR type defined below as a DataContract. 
[DataContract(Namespace="http://www.something.com/students/")]
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 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
{
       [MessageHeader()]
       public string AuthCode;

      [MessageBodyMember()]
      public int StudentId;
}

[MessageContract(IsWrapped = false)]
public class StudentInfoResponseMessage
{
      [MessageBodyMember()]
      public StudentInfo MyStudentInfo;
}

[ServiceContract] 
public interface IStudentService 
{
        [OperationContract] 
        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.

Reader of this article may also be interested in:

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Imran Abdul Ghani
Software Developer (Senior)
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 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 WCF Tutorials, Web Development.
Follow on   Twitter   Google+   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 26 Feb 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid