Click here to Skip to main content
13,006,002 members (35,549 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


19 bookmarked
Posted 15 Dec 2011

DataContract Vs MessageContract

, 15 Dec 2011
Rate this:
Please Sign up or sign in to vote.
DataContract Vs MessageContract

1. Comparison

Data Contracts

WCF data contracts provide a mapping function between .NET CLR types that are defined in code and XML Schemas Definitions defined by the W3C organization ( that are used for communication outside the service.

You can say “Data contract is a formal agreement between a service and a client that abstractly describes the data to be exchanged”. That is, to communicate, the client and the service do not have to share the same types, only the same data contracts. A data contract precisely defines, for each parameter or return type, what data is serialized (turned into XML) to be exchanged.

Message Contracts

Message contracts describe the structure of SOAP messages sent to and from a service and enable you to inspect and control most of the details in the SOAP header and body. Whereas data contracts enable interoperability through the XML Schema Definition (XSD) standard, message contracts enable you to interoperate with any system that communicates through SOAP.

Using message contracts gives you complete control over the SOAP message sent to and from a service by providing access to the SOAP headers and bodies directly. This allows use of simple or complex types to define the exact content of the SOAP parts.

2. Why Use MessageContract when DataContract is There?

Data contracts are used to define the data structure. Messages that are simply a .NET type, let's say in the form of POCO (plain old CLR object), and generate the XML for the data you want to pass.

Message contracts are preferred only when there is a need to “control” the layout of your message (the SOAP message); for instance, adding specific headers/footer/etc. to a message.

Sometimes, complete control over the structure of a SOAP message is just as important as control over its contents. This is especially true when interoperability is important or to specifically control security issues at the level of the message or message part. In these cases, you can create a message contract that enables you to use a type for a parameter or return value that serializes directly into the precise SOAP message that you need.

3. Why We Use MessageContract to Pass SOAP Headers?

Passing information in SOAP headers is useful if you want to communicate information “out of band” from the operation signature.

For instance, session or correlation information can be passed in headers, rather than adding additional parameters to operations or adding this information as fields in the data itself.

Another example is security, where you may want to implement a custom security protocol (bypassing WS-Security) and pass credentials or tokens in custom SOAP headers.

A third example, again with security, is signing and encrypting SOAP headers, where you may want to sign and/or encrypt some or all header information. All these cases can be handled with message contracts. The downside with this technique is that the client and service must manually add and retrieve the information from the SOAP header, rather than having the serialization classes associated with data and operation contracts do it for you.

4. Can’t Mix datacontracts and messagecontracts

Because message-based programming and parameter-based programming cannot be mixed, so you cannot specify a DataContract as an input argument to an operation and have it return a MessageContract, or specify a MessageContract as the input argument to an operation and have it return a DataContract. You can mix typed and untyped messages, but not messageContracts and DataContracts. Mixing message and data contracts will cause a runtime error when you generate WSDL from the service.

Hope this will help !!!


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


About the Author

Sandeep Ramani
Technical Lead
India India
I write software using Microsoft web technologies since 2008. I have successfully delivered software products for Fortune 500 companies and startups.

Microsoft Certified Technologies Specialist - Web Applications Development with Microsoft .NET Framework 4.

Awarded as Microsoft Community Contributor of the year 2011.

Received several awards at various forums and my various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website

Visit My Blog:

Area of Expertise:
C#, ASP.NET, Web Services, WCF, ASP.NET MVC, SQL Server, WEB API, AngularJS, jQuery

You may also be interested in...

Comments and Discussions

GeneralThanks for the info Pin
stixoffire29-Apr-15 7:22
memberstixoffire29-Apr-15 7:22 
QuestionSimilar things wrote 2 years before in this...... Pin
Pradip Kumar Sen21-Apr-15 22:44
memberPradip Kumar Sen21-Apr-15 22:44 
GeneralMy vote of 2 Pin
Member 1082774623-May-14 1:14
memberMember 1082774623-May-14 1:14 
GeneralMy vote of 5 Pin
Rob Philpott12-Sep-13 6:05
professionalRob Philpott12-Sep-13 6:05 
GeneralMy vote of 2 Pin
Member 145990211-Aug-13 4:21
memberMember 145990211-Aug-13 4:21 
GeneralRe: My vote of 2 Pin
Sandeep Ramani11-Aug-13 5:16
memberSandeep Ramani11-Aug-13 5:16 
QuestionProvide Illustrations Pin
meeram391-Mar-13 0:23
membermeeram391-Mar-13 0:23 
GeneralMy vote of 5 Pin
DynamicLynk3-Aug-12 7:04
memberDynamicLynk3-Aug-12 7:04 
GeneralMy vote of 5 Pin
Ashwini K Singh15-Jul-12 19:43
memberAshwini K Singh15-Jul-12 19:43 
GeneralMy vote of 3 Pin
sevenfox25-Feb-12 8:11
membersevenfox25-Feb-12 8:11 
QuestionQuery Pin
Kausik J. Nag20-Jan-12 19:45
memberKausik J. Nag20-Jan-12 19:45 
Questiongood post... Pin
Fabio Barbieri4-Jan-12 8:03
memberFabio Barbieri4-Jan-12 8:03 
QuestionUpdae required Pin
Marcus Kramer15-Dec-11 7:32
memberMarcus Kramer15-Dec-11 7:32 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170628.1 | Last Updated 15 Dec 2011
Article Copyright 2011 by Sandeep Ramani
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid