当我调用我的 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."}
最佳答案
据我所知,您有一个 WCF 函数,它有一个名为“myEntity”的参数。我假设 myEntity 的类型是一个用户定义的类,并且用 DataContract 属性装饰,这是应该的。我还假设 myEntity 的类型有一个成员字段,它是一个字符串数组。让我们假设所有这些都是真的(同样,如果您可以发布代码,那将非常有帮助)。
通常,字符串数组,即 string[],会很好地序列化。但是,在某些情况下(请参阅 here 和 here ),您可能必须将其添加到已知类型列表中,以便 WCF 正确序列化所有内容。
为此,请添加以下内容:
[DataContract]
[KnownType(typeof(string[]))]
public class YourClassNameHere
{
}
关于wcf - 为什么我会收到此 WCF 错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/800358/