向 WCF 数据合同添加字段会破坏客户吗? [英] Adding field to WCF data contract breaks clients?

查看:20
本文介绍了向 WCF 数据合同添加字段会破坏客户吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,它返回一个实现 IExtensibleDataObject 的类.我需要向这个类添加一个新字段.我更新了 DataContract 接口并对类进行了更改.现在,当我尝试运行客户端应用程序时,出现以下错误:

I have a WCF service that returns a class that implements IExtensibleDataObject. I need to add a new field to this class. I updated the DataContract interface and made the change to the class. Now when I try to run my client application I get the following error:

无法加载文件或程序集'xxx.yyyy.zzzz,版本=1.3.9.26111,文化=中立,PublicKeyToken=b09e2f3e9b5894f0' 或它的依赖项之一.位于程序集的清单定义确实与程序集引用不匹配.(来自 HRESULT 的异常:0x80131040)

Could not load file or assembly 'xxx.yyyy.zzzz, Version=1.3.9.26111, Culture=neutral, PublicKeyToken=b09e2f3e9b5894f0' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

WFC 类的 AssemblyVersion 已更改 - 这会破坏客户端吗?

The AssemblyVersion of the WFC class has been changed - does that break client?

生产中有使用此服务的客户端.如果可能的话,我不想让他们更新他们的服务参考并重新部署他们的客户端以进行这个简单的更改.

There are clients in production that use this service. I do not want to make them update their service reference and redeploy their clients for this simple change if possible.

推荐答案

这取决于您如何设置数据合同.

It depends on how you have set up your data contract.

在 WCF 中,使用所有默认值,您的服务将使用 DataContractSerializer (DCS) 将您的对象序列化为 XML.DCS 将按顺序序列化您的字段 - 首先是公共字段,然后是私有字段.在每个可见性组中,它将按名称的字母顺序对字段/属性进行排序.

In WCF, using all the defaults, your service will use the DataContractSerializer (DCS) to serialize your object into XML. The DCS will serialize your fields in order - first the public ones, then the private ones. Within each visibility group, it will order the fields/properties alphabetically by name.

因此,如果你引入一个新的公共属性 MiddleName 并且你已经有了 FirstNameLastName,你会没事的:旧的 XML本来

Thus, if you introduce a new public property MiddleName and you already had FirstName and LastName, you would be fine: the old XML would have been

<YourObject>
   ....
   <FirstName>.....</FirstName>
   <LastName>.....</LastName>
</YourObject>

而您的新属性只需在末尾添加一个新属性:

and your new one would just simply add a new property at the end:

<YourObject>
   ....
   <FirstName>.....</FirstName>
   <LastName>.....</LastName>
   <MiddleName>....</MiddleName>
</YourObject>

这种在最后添加一些东西"的更新应该可以正常工作,DCS 只会忽略 XML 中的其他标签.

Such an update "add something at the end" should work just fine, the DCS will just simply ignore additional tags in the XML.

然而:如果你引入了一个名为 Gender 的属性,它会; 并因此会破坏 XML 中数据的顺序,从而破坏数据契约 - 没有旧"客户端将能够调用您的新服务.

However: had you introduced a property called Gender, that would be stuck between <FirstName> and <LastName> and would thus break the order of the data in your XML and thus would break the data contract - no "old" client will be able to call your new service.

为了控制这一点,您可以在数据合同中的数据成员上放置特定的 Order= 属性:

In order to take control of this, you can put specific Order= attributes on your data members in your data contract:

[DataContract]
public class SomeAddress
{
   [DataMember(Order=0)]
   public string FirstName;

   [DataMember(Order=1)]
   public string LastName;
}

然后您可以轻松添加新属性 - 只需将其添加到列表末尾即可!

Then you could easily add a new property - just add it to the end of the list!

[DataContract]
public class SomeAddress
{
   [DataMember(Order=0)]
   public string FirstName;

   [DataMember(Order=1)]
   public string LastName;

   [DataMember(Order=2)]
   public string Gender;
}

因此,通过在您的数据协定上使用 Order= 属性,您可以控制您的 XML 布局,并且您可以对现有数据协定进行简单的扩展,不间断更新.

So by using the Order= attribute on your data contracts, you can take control of your XML layout, and you can make simple extensions of existing data contracts non-breaking updates.

有关更多背景知识和深入知识,您应该阅读MSDN 杂志网站上的 Windows Communication Foundation 中的序列化 - 强烈推荐.

For more background and in-depth know-how, you ought to read Serialization in Windows Communication Foundation on MSDN Magazine's web site - highly recommended.

这篇关于向 WCF 数据合同添加字段会破坏客户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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