2009-04-29 87 views
8

我在調用我的WCF服務時遇到以下錯誤。我在這裏錯過了什麼?爲什麼我得到這個WCF錯誤消息?

'System.String[]' with data contract name 
'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 
is not expected. Add any types not known statically to the list of known 
types - for example, by using the KnownTypeAttribute attribute or by adding 
them to the list of known types passed to DataContractSerializer.'. Please 
see InnerException for more details. 

{"There was an error while trying to serialize parameter 
http://tempuri.org/:myEntity. The InnerException message was 
'Type 'System.String[]' with data contract name 
'ArrayOfstring:http://schemas.microsoft.com/2003/10/Serialization/Arrays' 
is not expected. Add any types not known statically to the list of known 
types - for example, by using the KnownTypeAttribute attribute or by adding 
them to the list of known types passed to DataContractSerializer.'. 

Please see InnerException for more details."} 
+1

您可以將代碼發佈到您的服務和/或客戶端嗎? – 2009-04-29 00:19:28

+0

剛剛接觸到WCF ... myEntity是C#的商業對象...你可以讓我知道如何以及在哪裏向DataContractSerializer提供這種類型? – 2009-04-29 02:11:54

回答

12

從我所收集,你必須有一個名爲「myEntity所」參數WCF功能。我假設myEntity的類型是一個用戶定義的類,並用DataContract屬性進行裝飾,因爲它應該是。我還假設myEntity的類型有一個字符串數組的成員字段。假設所有這些都是真實的(再次,如果您可以發佈您的代碼,這將非常有幫助)。

通常情況下,字符串數組,即字符串[],將序列化很好。但是,在某些情況下(請參閱herehere),您可能需要將其添加到已知類型的列表中,以便WCF正確序列化所有內容。

要做到這一點,添加以下內容:

[DataContract] 
[KnownType(typeof(string[]))] 
public class YourClassNameHere 
{ 
} 
+0

轟炸目標馬特...作品像魅力!:-)感謝您的答案! – 2009-04-30 01:19:44

+0

很高興爲你效勞。 – 2009-04-30 03:44:10

+0

那麼我得到這個錯誤,我沒有自定義類?我只想傳遞一個恰好是字符串[]的參數。 – 2013-11-15 13:34:44

5

您還沒有發佈的代碼,所以我的回答是基於你有你試圖序列化類myEntity所假設。嘗試使用類別的已知類型屬性

例如,

[KnownType(typeof(myEntity))] 

你可以參考下面的MSDN鏈接: KnownTypeAttribute

+0

感謝您的答案....但它沒有爲我工作 – 2009-04-29 07:33:47

+0

史蒂夫,你可以請你張貼你的代碼樣本? – 2009-04-29 08:35:17

0

是。正如前一篇文章中所解釋的,如果您傳遞一個Type(它被定義爲DataContract)的數組,則會發生該問題。您將需要將此類的數組定義爲單獨類型並將其標記爲數據合約。

慣於Work`

[DataContract] 
Public class ABC{ 
} 

... 

SendData(ABC[]) 

`

什麼工作:

Public class Data{ public ABC[] prop{get;set;}} 
... 
SendData(Data); 
0

在我的情況下,添加[Serializable]屬性的myEntity所下課。然後問題出現在角色字符串數組的序列化中。

[Serializable] 
    [KnownType(typeof(string[]))] 
    public class MyEntity 
    { 
     ......... 
     public string roles[] 
     ......... 
    } 

[KnownType(typeof運算(字符串[]))],像變魔術一樣!