简体   繁体   English

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

[英]C# Web Service client cast error

I am trying to consume a Java web service but get an exception System.InvalidCastException: Cannot assign object of type ValueArrayType to an object of type ValueArrayType[] 我正在尝试使用Java Web服务,但遇到异常System.InvalidCastException:无法将ValueArrayType类型的对象分配给ValueArrayType []类型的对象

I am consuming a third party service so cannot change the service and have been informed that they can consume the service ok with php and java. 我正在使用第三方服务,因此无法更改服务,并被告知他们可以使用php和java正常使用服务。

Value Array type is complex type 值数组类型为复杂类型

 <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>

It is an element in the response DetailsType that can have several occurrences as it has max = unbound and is wrapped by a sequence attribute. 它是响应DetailsType中的一个元素,因为它具有max = unbound并由sequence属性包装,因此可以多次出现。

<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>

I have tried wsdll.exe and svrcutil.exe to try and generate client code. 我尝试过wsdll.exe和svrcutil.exe尝试生成客户端代码。 the ValueArrayType is defined in the generated code as an array. ValueArrayType在生成的代码中定义为数组。

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

an example of the data coming back is. 数据返回的一个例子是。

....
<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>
    .....

If I change the client code to public ValueArrayType ValueArray instead of an array then the client works but only gets the first ValueArray returned. 如果我将客户端代码更改为公共ValueArrayType ValueArray而不是数组,则客户端可以工作,但只会获取返回的第一个ValueArray。

Have tried suggestions from http://blogs.msdn.com/b/netcfteam/archive/2007/02/01/why-your-netcf-apps-fail-to-call-some-web-services.aspx . 尝试过http://blogs.msdn.com/b/netcfteam/archive/2007/02/01/why-your-netcf-apps-fail-to-call-some-web-services.aspx中的建议。

Update 更新资料
I have generated a WCF Service with the proxyclass generated from scvutil. 我已经使用从scvutil生成的proxyclass生成了WCF服务。 When I consume and check the xml with WCFTestCLient.exe. 当我使用WCFTestCLient.exe并检查xml时。

An Array type is sent back as 数组类型以以下形式发送回

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

I assume either the data being sent does not match the WSDL or there is a bug in the C# scvutil, or System.ServiceModel. 我假设发送的数据与WSDL不匹配,或者C#scvutil或System.ServiceModel中存在错误。

The problem is caused by incorrect xsi:type values that mislead WCF deserialization ( described here ). 该问题是由错误的xsi:type值引起的,该值会误导WCF反序列化( 在此进行介绍 )。

The workaround is to use OperationFormatUse.Literal instead of OperationFormatUse.Encoded on all operations. 解决方法是在所有操作上使用OperationFormatUse.Literal而不是OperationFormatUse.Encoded

Try to specify your element type inside the generated code 尝试在生成的代码中指定您的元素类型

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

More information is available at MSDN 有关更多信息,请访问MSDN。

Can you try something like this ? 你可以尝试这样的事情吗?

JavaServecie js= new JavaService(); JavaServecie js = new JavaService();

js.ValueArrayType arr= js.GetValues(..... js.ValueArrayType arr = js.GetValues(.....

if the class is public. 如果班级是公开的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM