为什么我会收到此 WCF 错误消息? [英] Why am I getting this WCF Error Message?

查看:42
本文介绍了为什么我会收到此 WCF 错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在调用 WCF 服务时收到以下错误.我在这里错过了什么?

I am getting the error below when I call my WCF service. What am I missing here?

'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 的类型有一个成员字段,它是一个字符串数组.让我们假设所有这些都是真的(同样,如果您能发布您的代码,那将非常有帮助).

From what I gather, you have a WCF function that has a parameter named 'myEntity'. I'm assuming that the type of myEntity is a user-defined class and is adorned with the DataContract attribute, as it should be. I'm also assuming that the type of myEntity has a member field that is a string array. Let's assume that all of this is true (again, it'd be really helpful if you could post your code).

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

Ordinarily, string arrays, i.e., string[], will serialize just fine. But, in some cases (see here and here), you may have to add it to the list of known types in order for WCF to serialize everything correctly.

为此,添加以下内容:

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

这篇关于为什么我会收到此 WCF 错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆