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

Ste*_*man 8 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."}
Run Code Online (Sandbox Code Playgroud)

Mat*_*vis 12

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

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

为此,请添加以下内容:

[DataContract]
[KnownType(typeof(string[]))]
public class YourClassNameHere
{
}
Run Code Online (Sandbox Code Playgroud)


Ras*_*dit 5

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

例如

[KnownType(typeof(myEntity))]
Run Code Online (Sandbox Code Playgroud)

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