Click here to Skip to main content
15,886,110 members
Articles / DataContract

DataContract Vs MessageContract in WCF

Rate me:
Please Sign up or sign in to vote.
4.85/5 (10 votes)
26 Feb 2014CPOL2 min read 66.8K   9   1
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:
C#
[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
C#
[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 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:
C#
[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 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:

License

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


Written By
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.

Comments and Discussions

 
PraiseGood explanation with example Pin
Prasanth_K22-Jun-16 19:06
Prasanth_K22-Jun-16 19:06 
Imran - Very good explanation with code sample. It will help if you can show how the SOAP message will actually look like in both the cases.

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.