[英]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:
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.