与KnownType字典的WCF问题 [英] WCF issues with KnownType for Dictionary
问题描述
我有一个服务实现以下DataMember:
[DataMember]
public Dictionary< string,List< ; IOptionQueryResult>> QueryResultItems {get;组;我有从IOQueryQueryResult继承的类OptionQuerySingleResult。现在,我明白,我需要使OptionQueryResult类型已知到服务,因此尝试以各种方式添加KnownType: [KnownType(typeof(Dictionary< string,OptionQuerySingleResult []>))]
[KnownType(typeof(Dictionary< string,List< OptionQuerySingleResult>))]
[KnownType (OptionQuerySingleResult)]
然而,这些方法中没有一个工作,在客户端,我要么反序列化失败或服务器简单地中止请求,导致连接中止错误。
有没有人知道什么是正确的方式来使其工作?
我想补充一点,如果我更改QueryResultItems定义来使用具体的类型,而不是接口,一切都很正常。
谢谢, p>
Tom
编辑:
我得到的例外是:
行1中的错误元素' http://schemas.microsoft.com/2003/10 /序列化/数组:anyType '包含映射到名称:OptionQuerySingleResult的类型的数据。解串器没有任何类型的知识,映射到这个名字。考虑使用DataContractResolver或将对应于OptionQuerySingleResult的类型添加到已知类型的列表中,例如使用KnownTypeAttribute属性或将其添加到传递给DataContractSerializer的已知类型列表。
但是,当我查看svcutil生成的客户端代理时,OptionQuerySingleResult被定义在其中:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(System.Runtime.Serialization,4.0.0.0)]
[System.Runtime.Serialization .DataContractAttribute(Name =OptionQuerySingleResult,Namespace =)]
[System.SerializableAttribute()]
public partial class OptionQuerySingleResult:Object,System.Runtime.Serialization.IExtensibleDataObject,System.ComponentModel.INotifyPropertyChanged
解决方案我想你会使用:
[已知类型(typeof(OptionQuerySingleResult)]
但是你还需要一个 [DataContract(您的OptionQuerySingleResult类上的名称=OptionQuerySingleResult)]
我认为这也依赖于SVCUTIL生成的客户端代理类。 EXE util。
I have a service that implements the following DataMember:
[DataMember]
public Dictionary<string, List<IOptionQueryResult>> QueryResultItems { get; set; }
I have the class "OptionQuerySingleResult" which inherits from IOptionQueryResult. Now, I understand that I need to make the OptionQueryResult type "known" to the Service and thus tried to add the KnownType in various ways:
[KnownType(typeof(Dictionary<string, OptionQuerySingleResult[]>))]
[KnownType(typeof(Dictionary<string, List<OptionQuerySingleResult>>))]
[KnownType(typeof(OptionQuerySingleResult)]
However, none of those approaches worked and on the client side I'm either getting that deserialization failed or the server simply aborted the request, causing a connection aborted error.
Does anyone have an idea on what's the proper way to get this to work?
I'd like to add that if I change the QueryResultItems definition to use the concrete type, instead of the interface, everything works just fine.
Thanks,
Tom
Edit:
The exception that I am getting is:
Error in line 1 position 524. Element 'http://schemas.microsoft.com/2003/10/Serialization/Arrays:anyType' contains data from a type that maps to the name ':OptionQuerySingleResult'. The deserializer has no knowledge of any type that maps to this name. Consider using a DataContractResolver or add the type corresponding to 'OptionQuerySingleResult' 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.
However, when I look at the client proxy that svcutil generates, "OptionQuerySingleResult" is definitely defined in it:
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "4.0.0.0")]
[System.Runtime.Serialization.DataContractAttribute(Name="OptionQuerySingleResult", Namespace="")]
[System.SerializableAttribute()]
public partial class OptionQuerySingleResult : object, System.Runtime.Serialization.IExtensibleDataObject, System.ComponentModel.INotifyPropertyChanged
解决方案 I think you would use:
[KnownType(typeof(OptionQuerySingleResult)]
But you also need a [DataContract(Name = "OptionQuerySingleResult")]
on your OptionQuerySingleResult class.
I think this also relies on your client proxy classes being generated by the SVCUTIL.EXE util.
这篇关于与KnownType字典的WCF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!