使用 C# 使用 Java Web 服务,错误无法生成临时文件 class

Consume Java Web Service with C#, error unable to generate temp class

我们正在尝试使用 C# 和 .Net framework 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

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

正在使用的 wsdl 是一个用 java 编写的库,由 BroadSoft Broadworks 类 使用来自其 XSD 文件的 jax-b 生成。它托管在 JBoss EAP 7 应用程序服务器 Java 1.8.

根据 salesforce link 和 "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 代码片段引起的:

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 link,似乎其他应用程序(例如 SoapUI 和 java 应用程序在数组数组或锯齿状数组方面没有问题,但是 C# .Net 已经有这个问题很长时间了一会儿。

我们 运行 遇到了很多关于上述问题的问题,尤其是 google 返回的结果很差,所以我们想帮助其他可能 运行 陷入困境的人这个问题在未来尤其是我们无法轻易修改事物的 java 方面,因为很多 jax-b 封送处理和取消封送处理正在进行并被自动处理。

销售人员 link 告诉我们问题出在哪里,link 提出了一个可能的解决方案: How can I fix the web reference proxy that Visual Studio generated to handle jagged arrays?

我的 .Net 合作伙伴 @lavilla 开始考虑实施上述代码更改,但我们真的很讨厌必须修改生成的代理代码,因为将来的任何更新都需要重新实施(预构建步骤但是会对此有所帮助)和他 运行 通过这个有趣的 Stack Exchange 问答: by @Synia。

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

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