【发布时间】: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