Click here to Skip to main content
12,695,365 members (27,933 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked

DataContract Vs MessageContract in WCF

, 26 Feb 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
DataContract Vs MessageContract in WCF
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. Let's 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 the above example, 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 the reader will have a good understanding about the difference between DataContract and MessageContract in WCF.

Readers of this Article May Also Be Interested In:


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) Emaratech
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, SharePoint for Dummies.

You may also be interested in...

Comments and Discussions

PraiseGood explanation with example Pin
prasanth.gun22-Jun-16 20:06
memberprasanth.gun22-Jun-16 20:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 26 Feb 2014
Article Copyright 2014 by Imran Abdul Ghani
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid