【问题标题】:Mono wsdl utility fails to process eBay's WSDL file for their SOAP APIMono wsdl 实用程序无法为其 SOAP API 处理 eBay 的 WSDL 文件
【发布时间】:2009-09-20 14:40:50
【问题描述】:

我在尝试处理 eBay 的 WSDL 文件时从 Mono 的 wsdl 实用程序收到此错误 -

(http://developer.ebay.com/webservices/latest/eBaySvc.wsdl)

$ wsdl eBaySvc.wsdl 
Web Services Description Language Utility
Mono Framework v2.0.50727.1433
Error: XmlSchema error: Ambiguous element label which is contained by -any- particle was detected: urn:ebay:apis:eBLBaseComponents:PreferenceLevel Related schema item SourceUri: file:///home/manger/projects/ebay/eBaySvc.orig.wsdl, Line 10296, Position 7.
Stack:
   at System.Xml.Schema.ValidationHandler.RaiseValidationEvent (System.Xml.Schema.ValidationEventHandler handle, System.Exception innerException, System.String message, System.Xml.Schema.XmlSchemaObject xsobj, System.Object sender, System.String sourceUri, XmlSeverityType severity) [0x00000] 
  at System.Xml.Schema.XmlSchemaObject.error (System.Xml.Schema.ValidationEventHandler handle, System.String message, System.Exception innerException, System.Xml.Schema.XmlSchemaObject xsobj, System.Object sender) [0x00000] 
  at System.Xml.Schema.XmlSchemaObject.error (System.Xml.Schema.ValidationEventHandler handle, System.String message) [0x00000]

在 Google 中搜索解决方案显示了将以 <xs:any ... 开头的元素更改为 <xs:any namespace="##other" ... 的建议——这无疑使 Mono 的 wsdl 实用程序能够处理该文件,从而生成一个 .cs 文件。但是,当我尝试实例化 Web 服务帮助程序类 (eBayAPIInterfaceService service = new eBayAPIInterfaceService();) 时,我的 C# 程序出现运行时异常:

Unhandled Exception: System.InvalidOperationException: There was an error reflecting type 'AddDisputeRequestType'. ---> System.InvalidOperationException: There was an error reflecting field 'DetailLevel'. ---> System.InvalidOperationException: There was an error reflecting type 'DetailLevelCodeType'. ---> System.InvalidOperationException: There was an error reflecting type 'System.Object'. ---> System.InvalidOperationException: There was an error reflecting type 'AbstractResponseType'. ---> System.InvalidOperationException: There was an error reflecting field 'Errors'. ---> System.InvalidOperationException: There was an error reflecting type 'ErrorType'. ---> System.InvalidOperationException: There was an error reflecting field 'ErrorParameters'. ---> System.InvalidOperationException: There was an error reflecting type 'ErrorParameterType'. ---> System.InvalidOperationException: There was an error reflecting field 'Any'. ---> System.InvalidOperationException: The element Any has been attributed with an XmlAnyElementAttribute and a namespace '', but no name. When a namespace is supplied, a name is also required. Supply a name or remove the namespace.                                           
  at System.Xml.Serialization.XmlReflectionImporter.ImportAnyElementInfo (System.String defaultNamespace, System.Xml.Serialization.XmlReflectionMember rmember, System.Xml.Serialization.XmlTypeMapMemberElement member, System.Xml.Serialization.XmlAttributes atts) [0x00000]                                                                                            
  at System.Xml.Serialization.XmlReflectionImporter.CreateMapMember (System.Type declaringType, System.Xml.Serialization.XmlReflectionMember rmember, System.String defaultNamespace) [0x00000]                                                   
  at System.Xml.Serialization.XmlReflectionImporter.ImportClassMapping (System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlRootAttribute root, System.String defaultNamespace) [0x00000]                                     
  --- End of inner exception stack trace ---

是 Mono 的 wsdl 工具有问题,还是 eBay 的 WSDL/模式有问题? - 我看到的几个论坛帖子说 WSDL 与架构不匹配,所以 Mono 做的事情是正确的,但我该如何解决它,以便我可以从 C# 实例化 Web 服务帮助程序类?

我的工具版本:

$ wsdl Web 服务描述语言实用程序 Mono 框架 v2.0.50727.1433

$ gmcs --version Mono C# 编译器版本 2.4.2.3

为 ErrorParameterType 生成的代码:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1433")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:ebay:apis:eBLBaseComponents")]
public partial class ErrorParameterType {

    private System.Xml.XmlElement[] anyField165;

    ... more class members follow ...

    /// <remarks/>
    [System.Xml.Serialization.XmlAnyElement(Namespace="")]
    public System.Xml.XmlElement[] Any {
        get {
            return this.anyField165;
        }
        set {
            this.anyField165 = value;
        }
    }
}

wsdl 在我的“修复”之后生成的 eBayAPIInterfaceService.cs 文件是 here

【问题讨论】:

  • ErrorParameterType.Any 的生成代码是什么样的?
  • @dtb :我已将 ErrorParameterType 的定义添加到问题文本中,但在源代码中找不到 ErrorParameterType.Any。我添加了一个指向正在生成的完整 .cs 文件的链接。该链接在同一目录中包含 Makefile 和我的 test.cs。
  • Namespace="" 似乎是罪魁祸首;我在 MSDN 上找到了一个报价(见我的回答)。

标签: c# web-services mono wsdl ebay-api


【解决方案1】:

我不知道这是否能解决您的问题,但您问题中的xs:any wildcard 缺少两个'#'

<xs:any namespace="##other" ...
                   ↑

生成的 C# 代码包含很多这样的定义:

[System.Xml.Serialization.XmlAnyElement(Namespace="")]
public System.Xml.XmlElement[] Any {
    get {
        return this.anyFieldXXX;
    }
    set {
        this.anyFieldXXX = value;
    }
}

来自MSDN

XmlAnyElementAttribute 应用于返回XmlElementXmlNode 对象数组的字段。根据对象是被序列化还是反序列化,可以以两种方式使用这样的字段。序列化时,对象作为 XML 元素或节点生成,即使它们在被序列化的对象中没有相应的成员(或多个成员)。如果在应用属性时指定了Name 属性值,则插入到数组中的所有XmlElementXmlNode 对象必须具有相同的元素名称和默认命名空间,否则将引发异常。 如果设置Namespace 属性值,则必须同时设置Name 属性,并且XmlElementXmlNode 对象也必须具有相同的名称和命名空间值。如果未指定 Name 值,则 XmlElementXmlNode 对象可以具有任何元素名称。

所以我猜解决方案是简单地删除 Namespace 属性值:

[System.Xml.Serialization.XmlAnyElement]

【讨论】:

  • 很好的发现 - 这是我的错字,我确实尝试了“##other” - 这让 wsdl 工具很开心,但是在运行我的简单 C# 程序时我仍然遇到运行时异常实例化 webservice 助手类
  • @dtb :感谢有关删除命名空间属性值的建议,这似乎解决了我得到的运行时异常,但现在我得到了一个新的运行时异常:“未处理的异常:System.InvalidOperationException : 在 eBayAPIInterfaceService 类中找不到成员 RequesterCredentialsField。” - 这很奇怪,因为 RequesterCredentialsField 是 eBayAPIInterfaceService 中的第一个成员。我不明白为什么找不到它。
  • RequesterCredentialsField 字段是私有的。 XmlSerialization 仅适用于公共属性/字段,即它应该使用公共 RequesterCredentials 属性而不是私有字段。
  • 谢谢,从生成的 XmlAnyElement 属性中删除命名空间并使用公共 RequesterCredentialsField 效果很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 2019-04-04
  • 2018-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多