Wednesday, April 24, 2013

Difference between Data Contract and Message Contract in WCF

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.

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 is it useful to pass information in SOAP headers with Message contract?

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.

3. 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.


  1. Hi! I know this is sort of off-topic but I had to ask.
    Does operating a well-established website like yours require a
    lot of work? I am brand new to writing a blog but I do write in my journal everyday.

    I'd like to start a blog so I will be able to share my personal experience and thoughts online. Please let me know if you have any ideas or tips for new aspiring blog owners. Thankyou!

    Look at my blog: bmi calculator women

  2. This is very interesting, You are a very skilled
    blogger. I have joined your rss feed and look forward to seeking more of your magnificent post.
    Also, I have shared your website in my social networks!

    Visit my weblog; Over the Counter Male Enhancement