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

cri*_*iel 8 c# wcf xmlserializer

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

这是我得到的错误:

System.InvalidOperationException : XmlSerializer attribute System.Xml.Serialization.XmlChoiceIdentifierAttribute is not valid in Item. Only XmlElement, XmlArray, XmlArrayItem, XmlAnyAttribute and XmlAnyElement attributes are supported when IsWrapped is true.

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

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

<xs:choice minOccurs="0">
<xs:element name="additionalSocInd" type="tns:BinaryExpressionType"/>
<xs:element name="skipServiceValidationInd" type="tns:BinaryExpressionType"/>
</xs:choice>
Run Code Online (Sandbox Code Playgroud)

相应的生成代码:

[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,
}
Run Code Online (Sandbox Code Playgroud)

当我注释掉上面的枚举及其所有引用时,该服务有效.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>
Run Code Online (Sandbox Code Playgroud)

由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>
Run Code Online (Sandbox Code Playgroud)

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


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

Yar*_*veh 8

尝试使用以下标志从命令行生成代理:

svcutil /wrapped /serializer:XmlSerializer http://wsdl_url/
Run Code Online (Sandbox Code Playgroud)