WCF: Serializing and Deserializing generic collections

Posted by Fabiano on Stack Overflow See other posts from Stack Overflow or by Fabiano
Published on 2010-04-01T16:49:39Z Indexed on 2010/04/01 16:53 UTC
Read the original article Hit count: 620

Filed under:
|
|
|
|

I have a class Team that holds a generic list:

[DataContract(Name = "TeamDTO", IsReference = true)]
public class Team
{
    [DataMember]
    private IList<Person> members = new List<Person>();

    public Team()
    {
        Init();
    }

    private void Init()
    {
        members = new List<Person>();
    }

    [System.Runtime.Serialization.OnDeserializing]
    protected void OnDeserializing(StreamingContext ctx)
    {
        Log("OnDeserializing of Team called");
        Init();
        if (members != null) Log(members.ToString());
    }

    [System.Runtime.Serialization.OnSerializing]
    private void OnSerializing(StreamingContext ctx)
    {
        Log("OnSerializing of Team called");
        if (members != null) Log(members.ToString());
    }

    [System.Runtime.Serialization.OnDeserialized]
    protected void OnDeserialized(StreamingContext ctx)
    {
        Log("OnDeserialized of Team called");
        if (members != null) Log(members.ToString());
    }

    [System.Runtime.Serialization.OnSerialized]
    private void OnSerialized(StreamingContext ctx)
    {
        Log("OnSerialized of Team called");
        Log(members.ToString());
    }

When I use this class in a WCF service, I get following log output

OnSerializing of Team called    
System.Collections.Generic.List 1[Person]

OnSerialized of Team called    
System.Collections.Generic.List 1[Person]

OnDeserializing of Team called    
System.Collections.Generic.List 1[ENetLogic.ENetPerson.Model.FirstPartyPerson]

OnDeserialized of Team called    
ENetLogic.ENetPerson.Model.Person[]

After the deserialization members is an Array and no longer a generic list although the field type is IList<> (?!) When I try to send this object back over the WCF service I get the log output

OnSerializing of Team called
ENetLogic.ENetPerson.Model.FirstPartyPerson[]

After this my unit test crashes with a System.ExecutionEngineException, which means the WCF service is not able to serialize the array. (maybe because it expected a IList<>)

So, my question is: Does anybody know why the type of my IList<> is an array after deserializing and why I can't serialize my Team object any longer after that?

Thanks

© Stack Overflow or respective owner

Related posts about wcf

Related posts about .NET