WCF列表< string>序列化/反序列化错误 [英] WCF List<string > serialization/deserialization error

查看:89
本文介绍了WCF列表< string>序列化/反序列化错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ServiceContract和DataContract类:

I have the following ServiceContract and DataContract classes:

[ServiceContract] 
public interface IWcfService 
{ 
    [OperationContract] 
    Response GetData(); 
} 

[DataContract]
public class Response 
{ 
    [DataMember] 
    public Dictionary<string, object> Data { get; set; } 
} 

当Response.Data字典的值是int,string,double或任何其他简单"原始类型时,WCF可以成功地序列化该对象.但是,当Response.Data字典的值的类型为List<字符串>,客户端在接收到数据并尝试反序列化时引发以下异常:

When the value of Response.Data dictionary is of type int, string, double or any other 'simple' primitive types, WCF can successfully serialize the object. But when the value of Response.Data dictionary is of type List< string>, the client threw following exception when itreceived the data and tried to deserialize it:

Message=The formatter threw an exception while trying to deserialize the message: 
There was an error while trying to deserialize parameter http://tempuri.org/:GetDataResult. 
The InnerException message was 'Error in line 1 position 990. 
    Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:Value' contains data from a type 
    that maps to the name 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:ArrayOfstring'. 
    The deserializer has no knowledge of any type that maps to this name. 
    Consider using a DataContractResolver or add the type corresponding to 'ArrayOfstring' 
    to the list of known types - for example, by using the KnownTypeAttribute attribute or 
    by adding it to the list of known types passed to DataContractSerializer.'.

我还试图将KnownType属性添加到ServiceContract和DataContract中,如下所示:

I've also tried to add KnownType attribute to the ServiceContract and DataContract like following:

[ServiceContract] 
[ServiceKnownType(typeof(List<string>))] 
[ServiceKnownType(typeof(Dictionary<string, string>))] 
[ServiceKnownType(typeof(Dictionary<string, List<string>>))] 
public interface IWcfService 
{ 
    [OperationContract] 
    [ServiceKnownType(typeof(List<string>))] 
    [ServiceKnownType(typeof(Dictionary<string, string>))] 
    [ServiceKnownType(typeof(Dictionary<string, List<string>>))] 
    Response GetData(); 
} 

[DataContract] 
[ServiceKnownType(typeof(List<string>))] 
[ServiceKnownType(typeof(Dictionary<string, string>))] 
[ServiceKnownType(typeof(Dictionary<string, List<string>>))] 
[KnownType(typeof(List<string>))] 
[KnownType(typeof(Dictionary<string, string>))] 
[KnownType(typeof(Dictionary<string, List<string>>))] 
public class Response 
{ 
    [DataMember] 
    public Dictionary<string, object> Data { get; set; } 
} 

但是这些都没有帮助.有人对此有任何想法吗?

But none of this helped. Anyone has any ideas on this?

已更新

数据如下:

Data = new new DIctionary<string, object> 
       { 
           {"_id", 12344}, 
           {"names", new List<string>{ "John", "Peter", "Jack"}}, 
           {"time", DateTime.Now} 
       }

我们使用Dictionary的原因<字符串,对象>: 服务器需要向客户端发送动态"数据的字典,这些数据可以是int,List,DataTime等.这将通过使用Dictionary来解决此问题,但同时也会丢失原始的类型信息.例如,客户端需要List并进行一些数据绑定以显示集合,因此List.ToString()在这种情况下将无济于事.

The reason why we used Dictionary< string, object>: Server needs to send to the client a dictionary of 'dynamic' data, which can be int, List, DataTime, etc. It will help revolve this issue by using Dictionary, but it also lose the original type information. For example, the client needs List and do some data binding to display the collection, so List.ToString() will not be helpful in this case.

推荐答案

感谢大家的投入.通过配置WCF以使用NetDataContractSerializer作为序列化程序(默认值是DataContractSerializer),我设法解决了这个问题. NetDataContractSerializer在进行序列化时会包含更多CLR类型信息,尽管它会影响性能(大约是序列化时间的两倍).

Thanks for everyone's inputs. I've managed to sovle the issue by configuring WCF to use NetDataContractSerializer as the serializer (default one is DataContractSerializer). NetDataContractSerializer will include more CLR type information when doing serialization, although it has performance hit (roughly double the serialization time).

这篇关于WCF列表&lt; string&gt;序列化/反序列化错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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