Click here to Skip to main content
15,868,016 members
Articles / All Topics

DataContract Vs MessageContract

Rate me:
Please Sign up or sign in to vote.
4.72/5 (21 votes)
15 Dec 2011CPOL3 min read 75.4K   19   13
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 (www.w3c.org/) 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 !!!

License

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


Written By
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 https://www.asp.net/

Visit My Blog:
https://ramanisandeep.wordpress.com/


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

Comments and Discussions

 
GeneralThanks for the info Pin
stixoffire29-Apr-15 7:22
stixoffire29-Apr-15 7:22 
QuestionSimilar things wrote 2 years before in this......https://social.msdn.microsoft.com/Forums/vstudio/en-US/ccbf6cb3-23dd-4e95-86a2-4ecdd390f9b1/difference-between-data-contract-and-message-contract-in-wcf?forum=wcf Pin
Pradip Kumar Sen21-Apr-15 22:44
professionalPradip Kumar Sen21-Apr-15 22:44 
GeneralMy vote of 2 Pin
Member 1082774623-May-14 1:14
Member 1082774623-May-14 1:14 
GeneralMy vote of 5 Pin
Rob Philpott12-Sep-13 6:05
Rob Philpott12-Sep-13 6:05 
GeneralMy vote of 2 Pin
adglopez11-Aug-13 4:21
professionaladglopez11-Aug-13 4:21 
GeneralRe: My vote of 2 Pin
Sandeepkumar Ramani11-Aug-13 5:16
Sandeepkumar Ramani11-Aug-13 5:16 
QuestionProvide Illustrations Pin
meeram391-Mar-13 0:23
professionalmeeram391-Mar-13 0:23 
GeneralMy vote of 5 Pin
DynamicLynk3-Aug-12 7:04
DynamicLynk3-Aug-12 7:04 
GeneralMy vote of 5 Pin
Ashwini K Singh15-Jul-12 19:43
Ashwini K Singh15-Jul-12 19:43 
GeneralMy vote of 3 Pin
sevenfox25-Feb-12 8:11
sevenfox25-Feb-12 8:11 
QuestionQuery Pin
Kausik J. Nag20-Jan-12 19:45
Kausik J. Nag20-Jan-12 19:45 
Questiongood post... Pin
Fabio Barbieri4-Jan-12 8:03
Fabio Barbieri4-Jan-12 8:03 
QuestionUpdae required Pin
fjdiewornncalwe15-Dec-11 7:32
professionalfjdiewornncalwe15-Dec-11 7:32 
I would suggest that you update your article to include proper references to your sources. I have found more than one already in only 5 minutes of looking. Credit those sources and that is fine, but don't just copy and paste someone else's work and claim it as your own. I won't vote on the article or flag it until I've given you a chance to rectify this.
I wasn't, now I am, then I won't be anymore.

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.