c# – System.InvalidOperationException:XmlSerializer属性System.Xml.Serialization.XmlChoiceIdentifierAttribute在Item中无效

我一直在尝试使用WCF连接到某些Web服务,但是当我尝试调用我需要的函数时,我一直收到错误.

这是我得到的错误

system.invalidOperationException:XmlSerializer属性System.Xml.Serialization.XmlChoiceIdentifierAttribute在Item中无效.当IsWrapped为true时,仅支持XmlElement,XmlArray,XmlArrayItem,XmlAnyAttribute和XmlAnyElement属性.

错误发生在它甚至调用实际服务之前,它甚至不会因为我试图调用方法而发生.问题在于在WCF生成的类中定义的另一个方法.

我已经能够将问题跟踪到XSD中用于定义WSDL的一段代码

<xs:choice minOccurs="0">
<xs:element name="additionalSocInd" type="tns:BinaryExpressionType"/>
<xs:element name="skipServiceValidationInd" type="tns:BinaryExpressionType"/>
</xs:choice>

相应的生成代码

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil","4.0.30319.1")]
[System.SerializableAttribute()]   [System.Xml.Serialization.XmlTypeAttribute(Namespace="http:integration.sprint.com/interfaces/manageSubscriberServices/v1/manageSubscr" +
    "iberServices.xsd",IncludeInSchema=false)]
public enum ItemChoiceType2
{
    additionalSocInd,skipServiceValidationInd,}

当我注释掉上面的枚举及其所有引用时,该服务有效. XSD中还有其他xs:choice语句似乎没有引起任何问题.

更新:
进一步调查显示,当您有以下情况时:

元素直接在序列元素内定义:

<xs:sequence>
<xs:element ... />
...
<xs:choice minOccurs="0">
<xs:element name="additionalSocInd" type="tns:BinaryExpressionType" />
<xs:element name="skipServiceValidationInd" type="tns:BinaryExpressionType" />
</xs:choice>
...
<xs:element ... />
</xs:sequence>

由svcutil生成的代理会导致上述错误.

当更改为如下所示:

<xs:sequence>
<xs:element ... />
...
<xs:element minOccurs="0" name="myChoiceType" type="tns:MyChoiceType" />
...
<xs:element ... />
</xs:sequence>
<xs:complexType name="MyChoiceType">
<xs:choice>
<xs:element name="additionalSocInd" type="tns:BinaryExpressionType" />
<xs:element name="skipServiceValidationInd" type="tns:BinaryExpressionType" />
</xs:choice>
</xs:complexType>

错误消失了.所以它可能是生成器(svcutil)生成代码错误.

我将需要调用WSDL中的所有方法,因此注释掉那些不起作用的方法不是一种选择.我需要在不改变WSDL(客户端,而不是我们的)的情况下使其工作.任何帮助,将不胜感激.

解决方法

尝试使用以下标志从命令行生成代理:
svcutil /wrapped /serializer:XmlSerializer http://wsdl_url/

相关文章

C#项目进行IIS部署过程中报错及其一般解决方案_c#iis执行语句...
微信扫码登录PC端网站应用的案例(C#)_c# 微信扫码登录
原文地址:http://msdn.microsoft.com/en-us/magazine/cc163...
前言 随着近些年微服务的流行,有越来越多的开发者和团队所采...
最近因为比较忙,好久没有写博客了,这篇主要给大家分享一下...
在多核CPU在今天和不久的将来,计算机将拥有更多的内核,Mic...