WCF/RESTful DataContract deserialization issue

天涯浪子 提交于 2019-12-12 04:31:53

问题


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 trying 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


回答1:


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.



来源:https://stackoverflow.com/questions/5021506/wcf-restful-datacontract-deserialization-issue

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!