2011-06-09 106 views
2

我使用kso​​ap2發送該請求,我的WCF web服務與Android客戶端:ksoap2和WCF複雜類型

<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:d="http://www.w3.org/2001/XMLSchema" xmlns:c="http://schemas.xmlsoap.org/soap/encoding/" xmlns:v="http://schemas.xmlsoap.org/soap/envelope/"><v:Header /> 
    <v:Body> 
     <HelloComplex xmlns="http://tempuri.org/" id="o0" c:root="1"> 
      <complex i:type="n0:SampleComplexType" xmlns:n0="http://tempuri.org/"> 
       <Value i:type="d:string">Hello!</Value> 
      </complex> 
     </HelloComplex> 
    </v:Body> 
</v:Envelope> 

,並收回此:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
     <s:Fault> 
      <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode> 
      <faultstring xml:lang="pt-BR"> 
       The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:complex. The InnerException message was 'Error in line 1 position 363. Element 'http://tempuri.org/:complex' contains data from a type that maps to the name 'http://tempuri.org/:SampleComplexType'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'SampleComplexType' to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding it to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details. 
      </faultstring> 
     </s:Fault> 
    </s:Body> 
</s:Envelope> 

但是,當我的其他應用( AspNew MVC)打電話給我:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> 
    <s:Body> 
     <HelloComplex xmlns="http://tempuri.org/"> 
      <complex xmlns:a="http://schemas.datacontract.org/2004/07/IssueCenter.Core" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
       <a:Value>C#VALUE!</a:Value> 
      </complex> 
     </HelloComplex> 
    </s:Body> 
</s:Envelope> 

ant it works。

如何解決我的Android客戶端請求?

說明:我知道命名空間差異,我已經試着做出同樣的結果。

回答

0

該服務不知道如何處理XML中的「ComplexType」類型屬性,因此您基本上需要告訴服務端序列化器它的含義。你可以通過幾種方式來實現:或者添加一個DataContractResolver,告訴序列化器這個未知類型ID的含義,或者你可以將類型(如果它存在於服務端)添加到「已知類型」列表中,那麼該服務確切地知道如何處理它。我認爲你應該使用DataContractResolver。

在框架的4.0版中,WCF引入了數據合約解析器。數據約定解析器提供了一些鉤子,它們允許你指定對象是什麼時候的對象,而不是「靜態地」定義一組已知類型(比如直接在KnownTypeAttribute中指定類型)被序列化或反序列化,CLR類型和XML中的名稱/名稱空間之間的映射將用於表示這種「未知」類型。爲此,您只需從DataContractResolver類繼承並提供映射邏輯。

我認爲這對你來說是更好的解決方案,因爲你可能不想在服務器端創建一個虛擬類型來處理這個問題,這是你必須做的事情,如果你想要使用KnownTypeAttribute。唯一需要記住的是,解析器使得序列化比使用「標準」已知類型特徵要慢(因爲已知類型是靜態的,它們可以被緩存並且不需要始終進行調用),所以要注意在使用解析器時,附加功能在執行時間方面具有代價。