Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF IList Serialization Issue

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?

like image 291
Nick Avatar asked Aug 18 '09 15:08

Nick


2 Answers

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

like image 139
Boris Modylevsky Avatar answered Sep 19 '22 06:09

Boris Modylevsky


I don't think you can do this. How will the serializer know what to desearialize to? Lots of things could implement an IList, and an interface doesn't have a constructor.

like image 28
Steve Avatar answered Sep 22 '22 06:09

Steve