WCF/RESTful DataContract 反序列化问题 [英] WCF/RESTful DataContract deserialization issue

查看:24
本文介绍了WCF/RESTful DataContract 反序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WCF 中实现一个安静的服务,但我遇到的问题是该服务无法反序列化传递给它的 xml.它试图将根元素映射到操作契约而不是数据契约.例如,对于以下 XML 数据包,

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>

它无法将 MyObject 反序列化为输入消息,因为它需要该级别的操作合同.

it's unable to deserialize MyObject as the input message since it expects the operation contract at that level.

我不想将所有字段都用作操作合约的参数,因为 1) 参数会超过 5 个,并且 2) 它没有为扩展数据留出空间.

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?

谢谢

推荐答案

我的解决方案是将我的操作合同更改为

My solution was to change my Operation Contract to

[OperationContract(Action="*")]
void ProcessMessage(Message message);

并使用

var msg = message.GetBody<MyObject>();

使用我现有的 DataContract.

with my existing DataContract.

更新:我实际上选择使用 XmlSerializer 进行反序列化,因为它允许调用服务重新排列 xml blob 中字段的顺序.

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.

这篇关于WCF/RESTful DataContract 反序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆