Why use messagecontract with datacontract?

Source: Internet
Author: User

Windows Communication Foundation (WCF): Why use messagecontract when datacontract is there?

While going through couple of WCF examples, this question was bogging
Down my empty skull for some time; and when I realized the difference
And usage of each in specific scenario, which basically wowould base
My requirement, I am posting this for if anyone who is going through
Mind-boggles of similar sort!

What is a contract, really? Wiki says:

"In law, a contract is a binding legal agreement that is
Enforceable in a court of law. [1] That is to say, a contract is
Exchange of promises for the breach of which the law will provide
Remedy ."

So what does that mean? In technical terms this means that these contracts (datacontract, messagecontract) bounds the IO client with specific terms and conditions; and only when met, the data is exchanged.

Msdn:
A Data contract is a formal agreement between a service and a client
That could actly describes the data to be exchanged. That is,
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
XML) to be exchanged. "-see datacontract types.


1. datacontracts descibes the datatypes used by a service.
2. datacontracts can be used to describe either parameters or return values.
3. datacontracts are unnecessary if the service only uses simple types
4. Data contracts enable interoperability through the XML schema definition (XSD) standard.

Note that, this enables the types to be described in metadata to enable the clients to InterOP with the service.


Following is how a basic datacontract is defined:

View plainprint?
  1. [Datacontract]
  2. Public class shape {}
  3. [Datacontract (name = "circle")]
  4. Public class circletype: Shape {}
  5. [Datacontract (name = "Triangle")]
  6. Public class triangletype: Shape {}


While, messagecontract (s)
Describes the structure of soap messages (since soap is context oriented
-Passing-on complete information about object) sent to/from a service
And enable you to inspect and control most of the details in the soap
Header and body.

1. Generic-messagecontract enables you to interoperate with any system that communicates through soap.
2. Control-using message contracts, we get complete control over soap
Messages sent to/from a service by having access to the SOAP Headers and
Bodies.
3. object context-This (soaping) allows use of simple or complex types to define the exact content of the soap.

Msdn:
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
Parameter or return value that serializes directly into the precise soap
Message that you need.

Following is a simplest message contract:

View plainprint?
  1. [Messagecontract]
  2. Public class bankingdepositlog
  3. {
  4. [Messageheader] public int numrecords
  5. [Messageheader] public depositrecord records [];
  6. [Messageheader] public int branchid;
  7. }


And above wowould result as following in SOAP Headers:

View plainprint?
  1. <Bankingdepositlog>
  2. <Numrecords> 3 </numrecords>
  3. <Records>
  4. <Depositrecord> record1 </depositrecord>
  5. <Depositrecord> record2 </depositrecord>
  6. <Depositrecord> record3 </depositrecord>
  7. </Records>
  8. <Branchid> 20643 </branchid>
  9. </Bankingdepositlog>

To see why it is useful to use messagecontract (s), that is, to pass information in SOAP Headers, you will have to dive into the soap advantages;
And Yep I am not getting into the disadvantages of SOAP (0: since their
Aren't too between-and those which are there are vary from scenario
Scenario. This obviusly does not mean you use soap all the time,
You definitely wowould require a good reason to use SOAP Headers over
Simple XML.

Enjoy coding!

Refered from: http://izlooite.blogspot.com/2010/01/wcf-why-use-messagecontract-when.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.