有DataContract為什麼要使用MessageContract

來源:互聯網
上載者: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 would base upon
my requirement, I am posting this for if anyone who is going through the
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 an
exchange of promises for the breach of which the law will provide a
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 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." - 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.   
  4. [DataContract(Name = "Circle")]  
  5. public class CircleType : Shape { }  
  6.   
  7. [DataContract(Name = "Triangle")]  
  8. 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 a
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 would 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 many - and those which are there are vary from scenario to
scenario. This obviously does not mean you use SOAP all the time, but
you definitely would 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.