Click here to Skip to main content
11,430,274 members (74,213 online)
Click here to Skip to main content
Technical Blog

Tagged as

DataContract Vs MessageContract

, 15 Dec 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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 ag

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, lets say in 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 !!!

@@@ Happy Diwali @@@

License

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

Share

About the Author

Sandeep Ramani
Technical Lead
India India
Sandeep is a passionate .NET developer.

He is also certified as Microsoft Certified Technologies Specialist - Web Applications Development with Microsoft .NET Framework 4.

He is also awarded as Microsoft Community Contributor of the year 2011.

He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.

He has done MCA from Gujarat University.

Visit his Blog:
http://ramanisandeep.wordpress.com/


Area of Expertise:
C#, ASP.NET , AJAX, Java script, JQuery, JSON, XML, Web Services, WCF, SSIS 2005, SQL Server 2005/2008, ASP.NET MVC

He is fond of movies, music, cricket, hockey and boxing.
Follow on   Twitter

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web03 | 2.8.150428.2 | Last Updated 15 Dec 2011
Article Copyright 2011 by Sandeep Ramani
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid