Learner
Learner

Reputation: 4751

How to use MessageContract correctly in the WCF OperationContracts?

Can someone point (or answer here, if possible) me to the material/reference which describes how to use MessageContract in WCF operations correctly. I have read this document.

I would like to know: 1) What limitations are there while using MessageContract as return value of the operation contract? 2) What if the class decorated withe the MessageContract has a property which returns an object of the class which is decorated with XmlRoot? 3) Are there any limitations or considerations if MessageContract is going to contain arrays or collections?

Upvotes: 2

Views: 3244

Answers (2)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

  1. The limitation is that once you use MessageContract for request description you have to use it for response as well and vice-versa. The only exception are operations returning void or accepting no parameters.
  2. WCF uses DataContractSerializer by default but you can switch it to XmlSerializer by marking contract, service or operation with XmlSerailazerFormat attribute. In such case serialization will ignore DataContract / DataMember attributes and start to use common Xml serialization attributes (including XmlRoot). What is most important is that MessageContract is not related to used serializer - it can be used with both DataContract and Xml serialization.
  3. I would pass the collection nested in other element. I'm not sure how if it is possible to pass unwrapped collection directly into soap:Body because it should be represented as multiple body elements which are handled separately by MessageContract - each is marked with MessageBodyMember attribute.

Upvotes: 3

DaveRead
DaveRead

Reputation: 3413

The MessageContract attribute enables/requires you to specify the format of the entire message for the operation, with finely grained control over how the message is serialized / deserialized, as opposed to the DataContract/DataMember attributes which control whether individual fields are included in the serialization.

The XmlRoot attribute is used by the System.Xml.XmlSerializer class when processing serializable classes; WCF uses the DataContractSerializer which does not use this attribute.

More info in this question: Why does the XmlRoot attribute gets ignored in WCF and how to overcome this

Upvotes: 1

Related Questions