views:

75

answers:

2

I'm trying to push a subset of a collection of data through WCF to be consumed by WCF - think paged data. As such, I want this collection to have one page's worth of data as well as a total number of results. I figured this should be trivial by creating a custom object that extends List. However, everything I do results in my TotalNumber property coming across as 0. All of the data gets serialized/deserialized just fine but that single integer won't come across at all.

Here's my first attempt that failed:

[Serializable]
public class PartialList<T> : List<T>
{
    public PartialList()
    {

    }

    public PartialList(IEnumerable<T> collection)
        : base(collection)
    {
    }

    [DataMember]
    public int UnpartialTotalCount { get; set; }

And here's my second attempt that failed in the exact same way:

[Serializable]
public class PartialList<T> : List<T>, ISerializable
{
    public PartialList()
    {

    }

    public PartialList(IEnumerable<T> collection)
        : base(collection)
    {
    }

    [DataMember]
    public int UnpartialTotalCount { get; set; }

    protected PartialList(SerializationInfo info, StreamingContext context)
    {
        UnpartialTotalCount = info.GetInt32("UnpartialTotalCount");
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("UnpartialTotalCount", UnpartialTotalCount);
    }

}

What's the deal here??

A: 

For WCF, you probably want to use the [DataContract] attribute on the class, rather than the ISerializable interface. WCF uses a different type of serialization, and a different convention for marking classes and members that should be serialized.

WCF may support [Serializable] or ISerializable, but I would recommend using just the [DataContract]/[DataMember]/etc. convention for WCF.

Andy White
The `[DataContract]` attribute causes runtime errors saying that it makes it an improper collection. I'm not sure of the exact error, but I've already tried that.
Jaxidian
A: 

According to this question, this is "By Design". :-(

Jaxidian