c# - WCF IList 序列化问题

标签 c# wcf web-services

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

[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();

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

最佳答案

对于 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();

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


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

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

关于c# - WCF IList 序列化问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1294560/

相关文章:

c# - 在数据库未定义的外键关系上配置 Entity Framework

c# - 更改 Thread.Priority 以使程序响应更快

.net - WCF:如何检查服务器是否可访问?

c# - 系统.IO.IOException : The handshake failed due to an unexpected > packet format?

c# - WCF Web 服务序列化错误 - 返回空值

c# - 当我的 ListBox 中有图像时,为什么会出现 OutOfMemoryException?

c# - 通过 GetAsync() 传递参数

c# - 将 JSON 对象发布到 WCF 服务对象始终为 null

c# - 如何创建对 TLS 1.2 Web 服务的 Web 服务引用?

wcf - 服务架构(WCF 和 Delphi)