WCF序列化顺序问题 [英] WCF serialization order issue

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

问题描述

最近我向 DataContract 添加了一个新属性,这破坏了我们 Java 客户端的 API,因为反序列化器无法识别新添加的属性.

Recently I added a new property to a DataContract, which broke the API for our Java clients because the deserializer did not recognize the newly added property.

显然,解决这个问题的方法是在新属性上设置 order 属性,以便它出现在 SOAP 消息的最后,然后反序列化器将简单地忽略它.

The solution to this apparently is to set the order attribute on the new property, so that it appears last in the SOAP message, then the deserializer will simply ignore it.

问题是这个新属性存在于基类中,而WCF总是先序列化基类属性,所以不管顺序属性如何,派生类属性总是会在后面出现.

The problem is that this new property exists in the base class, and WCF always serializes the base class properties first, so the derived class properties would always appear afterwards regardless of the order attribute.

我能想到的解决此问题的唯一方法是:

The only 2 ways I can think of to fix this are:

  1. 将属性及其所有逻辑复制到每个派生类
  2. 编写一个自定义序列化程序来全局"排序

第一个解决方案是最糟糕的,但也是最简单的.第二个难度更大,风险更大,但最好.

First solution is the worst, but easiest. Second is much more difficult and risky, but the best.

还有其他方法可以处理这种情况吗?是否可以强制默认 WCF 序列化程序以不同方式处理排序?

Are there any other ways I can handle this situation? Is it possible to force the default WCF serializer to handle ordering differently?

谢谢!

推荐答案

现在我已经采用了第一个解决方案,只需将基本属性设置为内部,并使用new"关键字将其隐藏在派生类中,现在它出现在 SOAP 消息的末尾.我还没有在客户端上测试过这个,但我会在这里更新.

For now I've gone with the first solution, by simply making the base property internal, and hiding it in the derived class with the "new" keyword, and now it appears at the end of the SOAP message. I haven't tested this with the clients yet, but will update here once I have.

基类:

internal bool? MyNewProperty
{
    get;
    set;
}

派生类:

[DataMember(Order = 2)]
public new bool? MyNewProperty
{
    get { return base.MyNewProperty; }
    set { base.MyNewProperty= value; }
}

如果我们想添加另一个应该向后兼容的新属性,Order 属性应该设置为 3(最佳做法是随着每次 API 更新而增加).

If we want to add another new property that should be backwards compatible, the Order attribute should be set to 3 (Best practice to increment with every API update).

更新:
即使我能够让元素出现在 SOAP 消息的最后,对于我们使用 Axis2 的一些客户来说,这仍然是一个突破性的变化,并且它不能优雅地处理反序列化新的无法识别的属性,即使它出现最后的.相关 SO 问题此处.

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

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