WCF/RESTful DataContract 反序列化问题
我正在尝试在 WCF 中实现 Restful 服务,但遇到问题,因为该服务无法反序列化传递给它的 xml。它试图将根元素映射到操作契约而不是数据契约。例如,对于以下 XML 数据包,
<MyObject>
<MyField1>asdf</MyField1>
<MyField2>1234</MyField2>
...
</MyObject>
它无法将 MyObject 反序列化为输入消息,因为它需要该级别的操作协定。
我不想只使用所有字段作为操作合约的参数,因为 1) 将有超过 5 个参数,2) 它没有为扩展数据留下空间。
我设置了一个行为扩展来记录传入的请求。我是否应该用根元素包装传入消息以便其正确反序列化?或者是否有一种不那么麻烦的方法来实现这项工作——而不强迫客户改变实现?
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的解决方案是将我的操作合同更改为
并使用我现有的数据合同反序列化消息
。
更新: 实际上,我选择使用 XmlSerializer 进行反序列化,因为它允许调用服务重新排列 xml blob 中字段的顺序。
My solution was to change my Operation Contract to
and deserialize the message using
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.