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

发布于 2024-12-03 10:13:55 字数 2208 浏览 0 评论 0原文

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

这是我收到的错误:

System.InvalidOperationException:XmlSerializer 属性 System.Xml.Serialization.XmlChoiceIdentifierAttribute 在项目中无效。当 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(这是客户的,而不是我们的)的情况下使其正常工作。任何帮助将不胜感激。

I have been trying to connect to some web services using WCF but I keep getting an error when I try to call the function I need.

This is the error I'm getting:


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.

The error happens before it even gets to calling the actual service and it doesn't even occur because of the method I'm trying to call. The issue is with another method that's defined in the WCF generated class.

I have been able to trace the issue to a section of code in the XSD that is used to define the WSDL:

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

The corresponding generated code:

[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,
}

When I comment out the above enum and all references to it, the service works. There are other xs:choice statements in the XSD that don't seem to cause any problems.


Update:
Further investigation revealed that when you have the following:

The element is defined directly inside of a sequence element:

<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>

The proxy generated by svcutil causes the error noted above.

When changed to look like this:

<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>

The error goes away. So it may be a bug with the code the generator (svcutil) generates.


I will need to call all the methods in the WSDL, so commenting out the ones that don't work isn't an option. And I need to get it working without changing the WSDL (which is the client's, not ours). Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗忘曾经 2024-12-10 10:13:55

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

svcutil /wrapped /serializer:XmlSerializer http://wsdl_url/

Try to generate the proxy from command line with these flags:

svcutil /wrapped /serializer:XmlSerializer http://wsdl_url/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文