简体   繁体   English

WCF的KnownType异常

[英]KnownType Exception with WCF

I have following Service Interface 我有以下服务界面

[ServiceContract]
public interface ICacheService
{
    [OperationContract]
    IEnumerable<CacheResponse> GetCache(IEnumerable<CacheRequest> requests);
}

[DataContract]
[KnownType(typeof(CacheItem))]
[KnownType(typeof(TreeItem))]
[KnownType(typeof(TreeTopGroup))]
[KnownType(typeof(TreeGroup))]
[KnownType(typeof(TreeView))]
[KnownType(typeof(ITreeItem))]
public class CacheResponse
{
    [DataMember]
    public IDictionary<string, CacheItem> CacheItems { get; set; }
    [DataMember]
    public IEnumerable<KCSLuceneDocument> LuceneDocuments { get; set; }
}

The request works so i won't post it. 该请求有效,所以我不会发布。 The Response only contains a DateTime, a String and a List of TreeItem. 响应仅包含一个DateTime,一个字符串和一个TreeItem列表。 Important part of the TreeItem Class TreeItem类的重要部分

public class TreeItem : ITreeItem 
{
    public ITreeItem Parent { get; set; }
    .
    . more stuff

}

As soon as the parent Property is set to something other then null, the client gets a System.ServiceModel.Dispatcher.NetDispatcherFaultException. 一旦将父属性设置为非null的其他值,客户端就会获得System.ServiceModel.Dispatcher.NetDispatcherFaultException。

Stating that the elment \\" http://schemas.datacontract.org/2004/07/Core.Base:_x003C_Parent_x003E_k__BackingField \\" contains Data of the Type \\" http://schemas.datacontract.org/2004/07/Core.Base:TreeItem \\" and that the deserializere doesn't know any Type with that name. 说明元素\\“ http://schemas.datacontract.org/2004/07/Core.Base:_x003C_Parent_x003E_k__BackingField \\”包含类型为“ http://schemas.datacontract.org/2004/07/Core ”的数据。 Base:TreeItem \\“,并且反序列化名称不知道具有该名称的任何Type。

I tried using the KnownType Attribute as well as the ServiceKnownType Attribute. 我尝试使用KnownType属性以及ServiceKnownType属性。 Both didn't help. 两者都没有帮助。 The only thing that worked is changing the type of Parent to TreeItem which i really don't want to do. 唯一有效的方法是将Parent的类型更改为TreeItem,我真的不想这样做。 Especially as it can contain ITreeItems in some other Locations which most likely also break the Service. 特别是因为它可以在其他一些位置包含ITreeItem,这很可能也会破坏服务。

Any idea how to solve the problem? 知道如何解决问题吗?

Do you have the [DataContract] attribute on TreeItem? 在TreeItem上是否具有[DataContract]属性? That would be required. 那将是必需的。 if the TreeItem does have a [DataContract] attribute set, I have a few other ideas, but it may require a bit more of the code to be posted. 如果TreeItem确实设置了[DataContract]属性,则我还有其他一些想法,但是可能需要发布更多代码。 So give it a try and let me know if I can be of more help. 因此,请尝试一下,让我知道是否可以提供更多帮助。

A hint as to what the other issues may be can be found on the MSDN for Data Contract Known Type. 可以在MSDN上找到有关数据合同已知类型的其他提示。

The KnownType is needed when the class marked with the Data Contract attribute meets one of the following: 当标记有“数据协定”属性的类满足以下条件之一时,需要KnownType:

  • The sent data contract is derived from the expected data contract. 发送的数据合同来自预期的数据合同。 For more information, see the section about inheritance in Data Contract Equivalence). 有关更多信息,请参见“数据合同等效性”中有关继承的部分。 In that case, the transmitted data does not have the same data contract as expected by the receiving endpoint. 在那种情况下,传输的数据与接收端点所期望的数据约定不同。
  • The declared type for the information to be transmitted is an interface, as opposed to a class, structure, or enumeration. 与类,结构或枚举相反,要传输的信息的声明类型是接口。 Therefore, it cannot be known in advance which type that implements the interface is actually sent and therefore, the receiving endpoint cannot determine in advance the data contract for the transmitted data. 因此,不能预先知道实际发送了实现该接口的类型,因此,接收端点无法预先确定所发送数据的数据协定。
  • The declared type for the information to be transmitted is Object. 声明的要传输信息的类型是Object。 Because every type inherits from Object, and it cannot be known in advance which type is actually sent, the receiving endpoint cannot determine in advance the data contract for the transmitted data. 因为每种类型都继承自Object,并且无法预先知道实际发送的是哪种类型,所以接收端点无法预先确定所传输数据的数据协定。 This is a special case of the first item: Every data contract derives from the default, a blank data contract that is generated for Object. 这是第一项的特殊情况:每个数据协定都源自默认值,即为对象生成的空白数据协定。
  • Some types, which include .NET Framework types, have members that are in one of the preceding three categories. 某些类型(包括.NET Framework类型)具有在前面三个类别之一中的成员。 For example, Hashtable uses Object to store the actual objects in the hash table. 例如,哈希表使用对象将实际对象存储在哈希表中。 When serializing these types, the receiving side cannot determine in advance the data contract for these members. 在序列化这些类型时,接收方无法预先确定这些成员的数据协定。

source 资源

If it is marked with [DataContract] you will likely need to add a [ServiceKnownType] attribute on the TreeItem class itself to show possibilities for the TreeItem.Parent property. 如果用[DataContract]标记,则可能需要在TreeItem类本身上添加[ServiceKnownType]属性,以显示TreeItem.Parent属性的可能性。 That is just a conjecture, I would need to see a bit more code first. 那只是一个推测,我首先需要查看更多代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM