簡體   English   中英

C#Web服務客戶端強制轉換錯誤

[英]C# Web Service client cast error

我正在嘗試使用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並由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>

我嘗試過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>
    .....

如果我將客戶端代碼更改為公共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生成的proxyclass生成了WCF服務。 當我使用WCFTestCLient.exe並檢查xml時。

數組類型以以下形式發送回

<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中存在錯誤。

該問題是由錯誤的xsi:type值引起的,該值會誤導WCF反序列化( 在此進行介紹 )。

解決方法是在所有操作上使用OperationFormatUse.Literal而不是OperationFormatUse.Encoded

嘗試在生成的代碼中指定您的元素類型

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

有關更多信息,請訪問MSDN。

你可以嘗試這樣的事情嗎?

JavaServecie js = new JavaService();

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

如果班級是公開的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM