WCF/RESTful DataContract deserialization issue
I am trying to implement a restful service in WCF, but am having issues in that the service is unable to deserialize the xml being passed to it. It is tryin开发者_开发技巧g to map the root element to the operation contract rather than to the data contract. For example, with the following XML packet,
<MyObject>
<MyField1>asdf</MyField1>
<MyField2>1234</MyField2>
...
</MyObject>
it's unable to deserialize MyObject as the input message since it expects the operation contract at that level.
I don't want to just use all the fields as parameters for the operation contract since 1) there would be more than 5 parameters, and 2) it does not leave room for extension data.
I have a behavior extension set up to log the incoming request. Should I just wrap the incoming message with a root element in order for it to deserialize properly? Or is there a less hacky way of making this work--without forcing the client to change implementation?
Thanks
My solution was to change my Operation Contract to
[OperationContract(Action="*")]
void ProcessMessage(Message message);
and deserialize the message using
var msg = message.GetBody<MyObject>();
with my existing DataContract.
Update: I actually chose to use XmlSerializer for deserialization, as it allows the calling service to rearrange the order of fields in the xml blob.
精彩评论