WCF生成的代理依赖于顺序,并且服务向序列中添加了新元素 [英] WCF generated proxy is order dependent and service added new elements to sequence

查看:46
本文介绍了WCF生成的代理依赖于顺序,并且服务向序列中添加了新元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们已经使用WCF和C#为现有SOAP Web服务构建了一个客户端.最近,Web服务已更新,我们的客户端停止工作.我认为该问题最好在此博客文章中进行解释-互操作性陷阱:XML元素的顺序" ,作者Yaron Naveh.

We've built a client to an existing SOAP web service using WCF and C#. Recently, the web service was updated and our client stopped working. The problem as I see it is best explained in this blog post - Interoperability Gotcha: Order of XML Elements by Yaron Naveh.

我将借用Yaron的例子来回答我的问题.最初,wsdl看起来像:

I'll borrow Yaron's example for my question. Originally, the wsdl looked like:

<s:element name="root">
 <s:complexType>
  <s:sequence>
   <s:element name="elem1" type="s:string" />
   <s:element name="elem2" type="s:string" />
  </s:sequence>
 </s:complexType>
</s:element">

WCF生成的代理使用显式元素排序,例如:

The WCF generated proxy used explicit element ordering like:

[XmlElement(Order=0)]
public string Elem1
{
...
}

[XmlElement(Order=1)]
public string Elem2
{
...
}

在更新中,将一个新元素添加到该类型,但将该元素添加到序列的中间.

In the update, a new element was added to to the type, but this element was added to the middle of the sequence.

<s:element name="root">
 <s:complexType>
  <s:sequence>
   <s:element name="elem1" type="s:string" />
   <s:element name="NewElement" type="s:string" />
   <s:element name="elem2" type="s:string" />
  </s:sequence>
 </s:complexType>
</s:element">

我的WCF代理无法反序列化添加了NewElement之后所排序的任何元素.

My WCF proxy cannot deserialize any elements that are ordered after the NewElement that was added.

Web服务的提供者希望此更改与较旧的客户端向后兼容.显然,我的客户是唯一停止工作的客户.

The provider of the web service expected this change to be backwards compatible with older clients. Apparently, my client is the only one that stopped working.

这是WSDL中的重大变化吗?

Is this a breaking change in the WSDL?

是否应在序列末尾添加新元素以防止破坏现有客户端?会使这个向后兼容吗?

Should new elements be added to the end of the sequence to prevent breaking existing clients? would that have made this backwards compatible?

如果我删除了XmlElement属性上的order参数,那么我的代理人可以为以后的类似变化做好更好的准备吗?如果我删除订单,该怎么办?

If I remove the order parameter on the XmlElement Attribute would my proxies be better prepared for future changes like this? What do I give up if I remove Order?

推荐答案

是的,这是其WSDL中的重大变化.您是正确的,将新元素添加到序列的末尾将使其向后兼容.

Yes, that is a breaking change in their WSDL. You are correct, adding the new elements to the end of the sequence would make it backward compatible.

如果他们希望消费者以任何顺序接受元素,则应该使用< xsd:all> 而不是< xsd:sequence> .并且当他们添加新元素时,它是必填元素,除非他们在其架构定义中添加 minOccurs ='0'属性以使其成为可选.

If they wanted consumers to accept elements in any order, they should have used <xsd:all> instead of <xsd:sequence>. And when they added the new element, it is a required element unless they add a minOccurs='0' attribute to its schema definition to make it optional.

通过在序列末尾添加< xsd:any> 元素作为将来元素的占位符,它们还可以使它们的架构更加向前兼容.

They could also make their schemas more forward-compatible by adding an <xsd:any> element to the end of their sequence as a placeholder for future elements.

如果从代理中删除订单",则可能遇到的主要问题是,它们是否允许按顺序重复具有相同名称的元素:

The main issue you may run into if you remove Order from your proxy is if they allow repetitions of elements with the same name in their sequence:

<s:element name="root">
 <s:complexType>
  <s:sequence>
   <s:element name="elem1" type="s:string" />
   <s:element name="elem2" type="s:string" />
   <s:element name="elem1" type="s:string" />
  </s:sequence>
 </s:complexType>
</s:element">

没有排序指示符,WCF将不知道哪个元素重复映射到哪个属性.如果他们是从.NET或Java之类生成XML的,那么这不太可能成为问题,因为它们通常将数组和List转换为单个父元素,并包装重复的子元素.

Without your ordering indicator, WCF wouldn't know which element repetition to map to which property. If they're generating their XML from something like .NET or Java, this is very unlikely to be a problem since those usually turn arrays and Lists into a single parent element that wraps repeating child elements.

使用< xsd:all> 的另一个好处是,它不允许重复相同名称的元素,从而避免了此问题.< xsd:sequence> 的功能"是允许多个具有相同名称的元素,仅根据它们在列表中的位置来区分.

Another benefit of using <xsd:all> is that it disallows repeating elements of the same name, thereby avoiding this issue. <xsd:sequence> has the "feature" of allowing multiple elements of the same name, differentiated only by their location in the list.

这篇关于WCF生成的代理依赖于顺序,并且服务向序列中添加了新元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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