WCF IList序列化问题

Nic*_*ick 6 c# wcf web-services

我有一个对象,其中包含一个通用的IList,它从WCF Web服务方法返回:

[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();
Run Code Online (Sandbox Code Playgroud)

当我在服务上调用此方法时,它会很好地执行整个方法,但最后它会抛出System.ExecutionEngineException而不会发生InnerException.我已经尝试返回一个具体的List <>对象,这似乎有效,但不幸的是我需要找到一个解决方法来返回一个IList.我需要输入任何属性来解决这个问题吗?

Bor*_*sky 1

对于 T 的每次用法,您必须在类定义上方的类定义上添加 KnownTypes 属性。如下所示:


[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();
Run Code Online (Sandbox Code Playgroud)

或者,您可以定义自己的集合类,它具有 TotalRows 属性,如下所示:


[KnownType(typeof(ContentItem))]
[DataContract(Name = "PageableList_Of_{0}")]
public class PageableResults<T> : EntityCollectionWorkaround<T>
{   
    [DataMember]
    public int TotalRows { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

EntityCollectionWorkaround 在这里定义:
http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html