WCF IList 序列化问题 [英] WCF IList Serialization Issue
问题描述
我有一个对象,其中包含从 WCF Web 服务方法返回的通用 IList:
[DataContract(Name = "PageableList_Of_{0}")]公共类 PageableResults{[数据成员]公共 IList<T>项目{得到;放;}[数据成员]公共 int TotalRows { 获取;放;}}【经营合同】PageableResultsListCI();
当我在服务上调用这个方法时,它会很好地执行整个方法,但最后它抛出一个 System.ExecutionEngineException 而没有 InnerException.我试过返回一个具体的 List<> 对象,这似乎有效,但不幸的是,我需要找到一种解决方法来返回 IList.我需要输入任何属性来解决这个问题吗?
对于 T 的每次使用,您必须在类定义上方的类定义上添加 KnownTypes 属性.像这样:
<预><代码>[已知类型(typeof(ContentItem))][DataContract(Name = "PageableList_Of_{0}")]公共类 PageableResults或者,您可以定义自己的集合类,它具有 TotalRows 属性,如下所示:
<预><代码>[KnownType(typeof(ContentItem))][DataContract(Name = "PageableList_Of_{0}")]公共类 PageableResults此处定义了 EntityCollectionWorkaround:
http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html
I have an object that has a generic IList in it that is being returned from a WCF web service method:
[DataContract(Name = "PageableList_Of_{0}")]
public class PageableResults<T>
{
[DataMember]
public IList<T> Items { get; set; }
[DataMember]
public int TotalRows { get; set; }
}
[OperationContract]
PageableResults<ContentItem> ListCI();
When I call this method on the service it executes the entire method fine, but at the very end it throws a System.ExecutionEngineException without an InnerException. I've tried returning a concrete List<> object and that seems to work, but unfortunately I need to find a workaround to return an IList. Are there any attributes I need to put in to solve this?
You'll have to add KnownTypes attribute on the class definition above your class definition for each usage of T. Like this:
[KnownType(typeof(ContentItem))]
[DataContract(Name = "PageableList_Of_{0}")]
public class PageableResults<T>
{
[DataMember]
public IList<T> Items { get; set; }
[DataMember]
public int TotalRows { get; set; }
}
[OperationContract]
PageableResults ListCI();
Alternatively you can define your own collection class, that has a TotalRows property, like this:
[KnownType(typeof(ContentItem))]
[DataContract(Name = "PageableList_Of_{0}")]
public class PageableResults<T> : EntityCollectionWorkaround<T>
{
[DataMember]
public int TotalRows { get; set; }
}
Where EntityCollectionWorkaround is defined here:
http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html
这篇关于WCF IList 序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!