【问题标题】:Consume Java Web Service with C#, error unable to generate temp class使用 C# 使用 Java Web 服务,错误无法生成临时类
【发布时间】:2017-07-19 15:23:30
【问题描述】:

我们正在尝试使用 C# 和 .Net 框架 4.0(也尝试过 2.0)使用 Java Web 服务,但遇到了一个奇怪的错误:

There was an error in serializing body of message userServiceAssignList1: 'Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'string[]' to 'string[][]'
error CS0029: Cannot implicitly convert type 'string[][]' to 'string[]'
error CS0029: Cannot implicitly convert type 'string[][]' to 'string[]'
'.  Please see InnerException for more details.

谷歌搜索基本上没有发现任何问题,因为它是一个相当常见的错误,在 C# 中错误地编码字符串和字符串数组。

C# 代码基本上是这样的,超级简单,其中“localhost”是 Web 服务或服务引用:

static void Main(string[] args)
{
    localhost.AdvancedBusinessTestIFClient abctic = new localhost.AdvancedBusinessTestIFClient();
    string[] services = {"Auto Attendant"};
    var resp = abctic.userServiceAssignList("echo", "SQA_P17615", services, null);

    Console.WriteLine(resp.ToString());
}

在研究中,我们发现了类似的问题: https://developer.salesforce.com/forums/?id=906F0000000AiPEIA0

所以它看起来是一个与微软的序列化和 wsgen 相关的错误,因为人们已经抱怨它超过 7 年了。它似乎与锯齿状数组有关,例如一个“字符串 [][]”。

所使用的 wsdl 是一个用 java 编写的库,由使用 jax-b 从其 XSD 文件生成的 BroadSoft Broadworks 类组成。它托管在 JBoss EAP 7 应用服务器 Java 1.8 中。

根据 salesforce 链接和“string[][]”提示,我们在 Reference.cs 文件中搜索“[][]”,果然找到了:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.6.1586.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="C")]
public partial class OCITable : object, System.ComponentModel.INotifyPropertyChanged {

    private string[] colHeadingField;

    private string[][] rowField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("colHeading", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, Order=0)]
    public string[] colHeading {
        get {
            return this.colHeadingField;
        }
        set {
            this.colHeadingField = value;
            this.RaisePropertyChanged("colHeading");
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true, Order=1)]
    //[System.Xml.Serialization.XmlArrayItemAttribute("col", typeof(string[][]), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
    [System.Xml.Serialization.XmlArrayItemAttribute("col", typeof(string[][]), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public string[][] row {
        get {
            return this.rowField;
        }
        set {
            this.rowField = value;
            this.RaisePropertyChanged("row");
        }
    }

    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

注意private string[][] rowField;

这是由 Java 代码的 sn-p 引起的:

public class OCITable {

    @XmlElement(required = true)
    protected List<String> colHeading;
    protected List<OCITableRow> row;


public class OCITableRow {

    @XmlElement(required = true)
    protected List<String> col;

所以我们可以看到它是一个列表的多维列表。

WSDL 块如下所示:

<xs:complexType name="OCITable">
    <xs:sequence>
        <xs:element maxOccurs="unbounded" name="colHeading" type="xs:string"/>
        <xs:element maxOccurs="unbounded" minOccurs="0" name="row" nillable="true" type="tns:OCITableRow"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="OCITableRow">
    <xs:sequence>
        <xs:element maxOccurs="unbounded" name="col" type="xs:string"/>
    </xs:sequence>
</xs:complexType>

返回到 SalesForce 链接,SoapUI 和 java 应用程序等其他应用程序似乎没有数组数组或锯齿数组的问题,但 C# .Net 有这个问题已经有一段时间了。

【问题讨论】:

  • 问题是什么?在目前的状态下,这更像是一个错误报告
  • @1blustone 感谢您的评论!这确实是一个错误报告,但答案最终提供了一个解决方案,我想在这里发布这个问答,因为我们花了大约一天的时间研究并试图找到解决方法!

标签: java c# web-services jagged-arrays


【解决方案1】:

我们在上述问题中遇到了很多问题,尤其是谷歌返回的结果很差,所以我们想帮助任何其他可能在未来遇到这个问题的人,特别是看到我们如何不容易修改事物的 java 方面,因为许多 jax-b 编组和取消编组正在进行并自动处理。

SalesForce 链接让我们知道问题所在,该链接提出了一个可能的解决方案: How can I fix the web reference proxy that Visual Studio generated to handle jagged arrays?

我的 .Net 合作伙伴 @lavilla 开始考虑实施上述代码更改,尽管我们真的很讨厌不得不修改生成的代理代码,因为未来的任何更新都需要重新实施(但是预构建步骤将帮助),他遇到了这个有趣的堆栈交换问答:C# Deserializing an XML with repeating tags@Synia。

感谢@Synia 也遇到了完全相同的问题,尽管如果您的问题中有“BroadSoft”或“BroadWorks”会很棒(有人可以添加 BroadSoft 和 BroadWorks 作为标签,因为我没有足够的分数)。这是我们正在寻找的确切解决方案,感谢@DBC 的回答和详尽的解释。

修改问题中之前显示的 Reference.cs 代码以准确反映@DBC 推荐的内容,效果完美!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-25
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多