Search code examples
.netwcfdatacontractserializer

BindingList<T> as DataMember


I have a CLR-class that I want to send via a WCF service (Net TCP Binding). Both the client and server use the same data classes and service interfaces. There are no stubs generated. Here are the basic parts of two classes:

[DataContract]
public class ContainerClass
{
    //some primitive data with [DataMember] ...

    [DataMember] /**/
    public virtual BindingList<ItemClass> Items{ get; private set; }
}

[DataContract]
public class ItemClass
{
    //some primitive data with [DataMember] ...

    public ContainerClass Parent { get; set; } // (no [DataMember])
}

The data is correctly transmitted, if I omitt the [DataMember] attribute of the BindingList (marked with /**/). Of course, whithout the items.

As soon as I add the [DataMember], a call to the service method, which returns an object of type ContainerClass fails with the following error message:

The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.

A contract mismatch is impossible by design. There is probably an internal server error of which I am not notified.

How can I solve this problem and make the server send the items to the client?


Solution

  • Most likely BindingList<> fails to serialize. To get more detailed error you need to enable tracing. See this post on how to do it.

    It's usually not good idea to use complex collection classes within your DTO. Consider arrays/lists instead, you can always reconstruct BindingList<> on client side.