none
What is the difference between data contract & message contract in WCF RRS feed

  • Question

  • i am new and learning wcf. so just guide me what is data contract & what is message contract in WCF ?

    when we should think for data contract & when we should go for message contract in WCF ?

    if possible give me a small sample code on data contract & message contract which help me to visualize what are they & when which one need to used? thanks

    Sunday, February 2, 2014 6:13 PM

Answers

  • Hi,

    >>what is data contract & what is 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 (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.

    >>when we should think for data contract & when we should go for message contract in WCF

    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.

    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.

    >>if possible give me a small sample code on data contract & message contract which help me to visualize what are they & when which one need to used

    Please try to check this article for better understanding:
    #Understanding Contracts in WCF:
    http://www.codeproject.com/Articles/664238/Understanding-Contracts-in-WCF .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, February 6, 2014 9:36 AM
    Moderator