tags:

views:

10

answers:

1

I have Restful wcf service that returns the following class, but the Total and Count fields become 0 when it reaches the client side. But they have the correct values on the server side.

 public class Groups : List<Group>
{
    private int total;
    private int start;

    /// <summary>
    /// Total number of Groups in the result set irrespective of the paging
    /// </summary>
    public int Total
    {
        get
        {
            return total;
        }
        set
        {
            total = value;
        }
    }

    /// <summary>
    /// Index (in the full non paged result set) of the first group in the set. 
    /// </summary>
    public int Start
    {
        get
        {
            return start;
        }
        set
        {
            start = value;
        }
    }


}

The problem is when client receives the return value from the service call, the Start and Total fields are always 0. But when debug the service's code it returns the correct value but by the time it comes to the client side they have become 0. But the List of the base class is returned properly (it doesn't get lost)

When I return the Group class, which is a simple class (not derived from anything), it is also returned properly.

The problem happens only with the Public fields of the groups collection class.

So I think this is a deserialization issue and tried adding the [Datamember] attribute, [seriaizable] and implementing ISerializable but nothing worked.

Any help will be greatly appreciated.

Thanks

A: 

Ok this is what I did if anyone else encountered this question.

As I found out this is the default behavior of the DataContractSerializer, also it is not a good design idea to add other properties to collections. The collections should be just a collection of items only. So what I did was to modify the code as below and it works

public class Groups

{ private int total; private int start; private List groups;

public int Total
{
  get { return total; }
  set { total = value; }
}

public int Start
{
  get { return start; }
  set { start = value; }
}

public List<Group> Values
{
  get { return groups; }
  set { groups = value; }
}

}

Amila