Serialization WCF IList

I have an object that has a common IList in it, which is returned from the 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 in the service, it completely executes the whole method, but at the very end it throws a System.ExecutionEngineException without an InnerException. I tried returning a specific List <> object and it seems to work, but unfortunately I need to find a workaround to return an IList. Are there any attributes I need to contribute to solve this problem?

+6
c # web-services wcf
source share
4 answers

You will need to add the KnownTypes attribute to the class definition above your class definition for each use of T. For example:

 [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 the TotalRows property, for example:

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

EntityCollectionWorkaround is defined here:
http://borismod.blogspot.com/2009/06/v2-wcf-collectiondatacontract-and.html

+1
source share

I do not think you can do this. How does a serializer know what to deserialize to? Many things can be implemented by IList, and the interface does not have a constructor.

+1
source share

This seems to be a bug in WCF, which is fixed in .NET 4. However, there are several workarounds in this thread:
http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=433569

Summary:
- Place assemblies containing DataContracts in the GAC.
- Set LoaderOptimization to SingleDomain.

+1
source share

Inherit from PageableResults to create a private closed subclass, in your case PageableContentItem or something like that, and use this as the return type. Typically, web services use an XML serializer, and they need to know everything in advance, so you cannot return interface types either.

  public class PageableContentItem : PageableResults<ContentItem> { } [OperationContract] PageableContentItem ListCI(); 
0
source share

All Articles