WCF 禁用反序列化顺序敏感性 [英] WCF Disable Deserialization Order Sensitivity

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

问题描述

在非 .NET 客户端和 .NET WCF 服务之间传递序列化对象时,我经常遇到问题.

I have a recurring problem when passing Serialized objects between non-.NET Clients, and .NET WCF Services.

WCF 反序列化对象时,严格依赖于属性的顺序.

When WCF Deserializes objects, it is strictly dependant upon the order of properties.

也就是说,如果我将类定义为:

That is, if I define my class as:

public class Foo 
{ 
  public int ID { get; set; } 
  public int Bar { get; set; } 
} 

然后WCF将对象序列化为:

Then WCF Will serialize the object as as:

<Foo>
  <Bar>123</Bar>
  <ID>456</ID>
</Foo>

注意:属性按字母顺序排列.

Note: The properties are serialized in alphabetical order.

如果您尝试反序列化一个 BarID 的位置交换的对象,WCF 会将位置不正确的元素视为 null.

If you attempt to Deserialize an object which has the positions of Bar and ID swapped, WCF will treat the incorrectly positioned elements as null.

虽然我知道我可以使用 DataMember 属性并强制进行特定排序,但我想减少我必须调试字段神秘地"为空的问题的次数.

Whilst I know I can use the DataMember attribute, and force a specific ordering, I want to reduce the number of times I have to debug issues where fields are 'mysteriously' null.

所以,我的问题是:在反序列化对象时,您能否告诉 WCF 反序列化器忽略字段的顺序.

So, my question is: Can you tell the WCF Deserializer to ignore the order of fields, when deserializing objects.

推荐答案

这里有一个旧线程:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/a891928b-d27a-4ef2-83b3-ee407c6b9187

看起来唯一的选择是交换序列化程序,但随后它变成了选择,这更烦人.

Looks like the only option is to swap the serializer, but then it becomes opt in which is even more annoying.

您可以编写自己的序列化程序来对元素重新排序,然后将其传递给 DataContractSerializer.

you could write your own serializer to re-order the elements and then pass it on to DataContractSerializer.

这篇关于WCF 禁用反序列化顺序敏感性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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