2

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?

Learner
  • 4,661
  • 9
  • 56
  • 102

2 Answers2

3
  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.
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • Thanks for 'to the point' answer. +1.I've a `MessageContract` which has `MessageBodyMemeber` of the type which is decorated with `XmlRoot` attribute. That `XmlRoot` class has property which gives `Collection` of another type which has `XmlElement`s. In this case, when I use `XmlSerializerFormat`, I get errors while browsing service. What is wrong in this scenario? When I don't use it, I can browse the service but at run time, in code, I get the exception http://stackoverflow.com/questions/6263310/problem-while-using-messagecontract-attribute-exception-end-element-body-f . Pls help – Learner Jun 13 '11 at 16:56
1

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

Community
  • 1
  • 1
DaveRead
  • 3,371
  • 1
  • 21
  • 24
  • Thanks for referring to the SO question. It was helpful. In my application when I use `MessageContract` which has property returning object of the `XmlRoot` class I get exception. Exception is: ** The formatter threw an exception while trying to deserialize the message: Error in deserializing body of request message for operation 'MyOperation'. End element 'Body' from namespace 'http://schemas.xmlsoap.org/soap/envelope/' expected. **. Have you seen such exception in scenarios of the MessageContract ? – Learner Jun 09 '11 at 12:34