为什么我收到此WCF错误消息?

时间:2009-04-29 00:12:52

标签: wcf web-services

当我调用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."}

4 个答案:

答案 0 :(得分:12)

从我收集的内容来看,你有一个WCF函数,它有一个名为'myEntity'的参数。我假设myEntity的类型是用户定义的类,并且应该使用DataContract属性进行装饰。我还假设myEntity的类型有一个成员字段,它是一个字符串数组。让我们假设所有这一切都是正确的(再次,如果您可以发布您的代码,它会非常有用。)

通常,字符串数组,即string [],会很好地序列化。但是,在某些情况下(请参阅herehere),您可能必须将其添加到已知类型列表中,以便WCF正确序列化所有内容。

为此,请添加以下内容:

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

答案 1 :(得分:5)

您尚未发布代码,因此我的答案基于您尝试序列化的类myEntity的假设。尝试使用类的KnownTypeAttribute

e.g。

[KnownType(typeof(myEntity))]

您可以参考以下MSDN链接: KnownTypeAttribute

答案 2 :(得分:0)

是。如前一篇文章中所述,如果传递Type的数组(定义为DataContract),则会出现问题。您需要将此类的数组定义为单独的类型,并将其标记为数据协定。

不工作?

[DataContract]
Public class ABC{
}

...

SendData(ABC[])

`

什么可行:

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

答案 3 :(得分:0)

在我的例子中,在将[Serializable]属性添加到MyEntity类之后。然后问题出现了角色字符串数组的序列化。

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

[KnownType(typeof(string []))]像魔术一样工作!