gpt4 book ai didi

C# Web 服务客户端转换错误

转载 作者:搜寻专家 更新时间:2023-11-01 03:57:07 24 4
gpt4 key购买 nike

我正在尝试使用 Java Web 服务,但出现异常 System.InvalidCastException:无法将 ValueArrayType 类型的对象分配给 ValueArrayType[] 类型的对象

我正在使用第三方服务,因此无法更改该服务,并且已被告知他们可以使用 php 和 java 正常使用该服务。

值数组类型是复杂类型

 <xsd:complexType name="ValueArrayType">
<xsd:sequence>
<xsd:element name="ValueName" type="xsd:string"/>
<xsd:element name="ValueType" type="xsd:string"/>
<xsd:element name="ValueValue" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>

它是响应 DetailsType 中的一个元素,可以多次出现,因为它具有 max = unbound 并且由序列属性包装。

<xsd:complexType name="DetailsType">
<xsd:sequence>
<xsd:element name="Id" type="xsd:int"/>
<xsd:element name="MobileName" type="xsd:string"/>
<xsd:element name="ValueArray" type="tns:ValueArrayType" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>

我已尝试使用 wsdll.exe 和 svrcutil.exe 来生成客户端代码。ValueArrayType 在生成的代码中定义为数组。

public ValueArrayType[] ValueArray
{
get
{
return this.valueArrayField;
}
set
{
this.valueArrayField = value;
}
}

返回数据的一个例子是。

....
<Details xsi:type="tns:DetailsType">
<Id xsi:type="xsd:int">9999</Id>
<ValueArray xsi:type="tns:ValueArrayType">
<ValueName xsi:type="xsd:string">Count</ValueName>
<ValueType xsi:type="xsd:string">numeric</ValueType>
<ValueValue xsi:type="xsd:string">11</ValueValue>
</ValueArray>
<ValueArray xsi:type="tns:ValueArrayType">
<ValueName xsi:type="xsd:string">Start</ValueName>
<ValueType xsi:type="xsd:string">numeric</ValueType>
<ValueValue xsi:type="xsd:string">31</ValueValue>
</ValueArray>
<ValueArray xsi:type="tns:ValueArrayType">
<ValueName xsi:type="xsd:string">A1</ValueName>
<ValueType xsi:type="xsd:string">numeric</ValueType>
<ValueValue xsi:type="xsd:string">23</ValueValue>
</ValueArray>
<ValueArray xsi:type="tns:ValueArrayType">
<ValueName xsi:type="xsd:string">A2</ValueName>
<ValueType xsi:type="xsd:string">numeric</ValueType>
<ValueValue xsi:type="xsd:string">0</ValueValue>
</ValueArray>
<ValueArray xsi:type="tns:ValueArrayType">
<ValueName xsi:type="xsd:string">X1</ValueName>
<ValueType xsi:type="xsd:string">numeric</ValueType>
<ValueValue xsi:type="xsd:string">0</ValueValue>
</ValueArray>
.....

如果我将客户端代码更改为 public ValueArrayType ValueArray而不是数组然后客户端工作但只得到返回的第一个 ValueArray。

已尝试来自 http://blogs.msdn.com/b/netcfteam/archive/2007/02/01/why-your-netcf-apps-fail-to-call-some-web-services.aspx 的建议.

更新
我已经使用从 scvutil 生成的代理类生成了一个 WCF 服务。当我使用 WCFTestCLient.exe 使用和检查 xml 时。

Array 类型被发回为

<ValueArray>
<ValueArrayType>
<ValueName>a</ValueName>
<ValueType>string</ValueType>
<ValueValue>1</ValueValue>
</ValueArrayType>
<ValueArrayType>
<ValueName>a</ValueName>
<ValueType>string</ValueType>
<ValueValue>2</ValueValue>
</ValueArrayType>
</ValueArray>

我假设发送的数据与 WSDL 不匹配,或者 C# scvutil 或 System.ServiceModel 中存在错误。

最佳答案

尝试在生成的代码中指定您的元素类型

[XmlElement(ElementName = "ValueArray", Type = typeof(ValueArrayType), Namespace = "YourSchemaNamespace")]
public ValueArrayType[] ValueArray
{
get
{
return this.valueArrayField;
}
set
{
this.valueArrayField = value;
}
}

更多信息请访问 MSDN

关于C# Web 服务客户端转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9695083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com