DataContract vs Message Contract
Though I have read some WCF articles about mes开发者_JAVA百科sage contract, but I am not able to comprehend what is the real use of message contract. Can I use MessageContract
instead of DataContract
or in complement to DataContract
?
A DataContract is a description of a type that is exposed and supported by your service. A MessageContract is an abstraction over a SOAP message that allows you to explicitly dictate the structure of the underlying message.
They are meant to compliment each other and serve different purposes.
Marc Gravell answered to a similar question WCF: MessageContract, DataContract ... Confused?
One of the advantages of message-contracts is that you can set privacy against members, but in many cases this isn't necessary. In such cases, I prefer to keep the contract as simple as possible, just as a data-contract.
Using DataContract, the service can expose the types that it interchanges. But the XML (SOAP) that is interchanged is not controlled (though impacted) by it. While MessageContract can be used to explicitly define how the XML (SOAP message) will be structured.
Typically when building Windows Communication Foundation (WCF) applications, developers pay close attention to the data structures and serialization issues and do not need to concern themselves with the structure of the messages in which the data is carried. For these applications, creating data contracts for the parameters or return values is straightforward.
However, 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 specify the structure of the precise SOAP message required.
For more information : https://msdn.microsoft.com/en-us/library/ms730255(v=vs.110).aspx
One of the best part in in message contract that it would always help you to put additional security or to write a wrapper for Authentication purpose to make authenticated call to service(s)(this could include both for WCF and WS) while client makes a call to service.
Message contract also helps to diagnosis the communication between a client and service by introducing different message inspector.
精彩评论