IList and Round-trip Serialization Issue in WCF

| Comments

Problem

WCF throws System.ExecutionEngineException when deserializing and consuming data contracts with IList based attributes.

Forces

  • Collection is changed as immutable when using IList
  • Unable to convert it to List
  • Unable to send it back to service consumer if required

Solution

Let us define a data contract Dump which contains IList of InternalDump.

1
2
3
4
5
6
7
 
[DataContract]
public class InternalDump
{
    [DataMember]
    public string Part;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
[DataContract]
public class Dump
{
    [DataMember]
    public string Name;
    [DataMember]
    public IList<InternalDump> Parts;
 

    public Dump()
    {
        Parts = new List<InternalDump>();
    }
}

I’ve declared a simple WCF service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[ServiceContract]
public interface IService1
{
    [OperationContract]
    Dump GetData(Dump d);
}
 

public class Service1 : IService1
{
    public Dump GetData(Dump d)
    {       
        return d;
    }
}

 You are noticed that GetData just returns the deserialized “d” as a return value to the consumer.  Being a .NET client, the consumer of this service as

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Dump d = new Dump();
d.Name = "Roundtrip";
InternalDump id = new InternalDump();
id.Part = "Client";
d.Parts.Add(id);
 

ServiceReference1.Service1Client sc = new DumpConsole.ServiceReference1.Service1Client();
Dump sd = sc.GetData(d);

foreach (InternalDump id2 in sd.Parts)
{
    Console.Write(id2.Part);
}

At the consumer side, a new instance of Dump and InternalDump have been created, serialized and send it as input parameter for GetData.  At the service side, the probelm is when deserializing the IList, WCF internally convert it into InternalDump[] which makes all the problem 1 and 2 mentioned in Forces section.

The problem is not in any of the above code, instead WCF itself.  The world’s so extensible ESB (enterprise service bus) framework assumed and convert IList into T[] for platform neutral.

Unfortunately, do not know the root cause of problem 3 mentioned in “Forces” section.  The solution is not the technical one, but a guideline.

Don’t use IList for the above mentioned probelms.  Instead use List.

Hope WCF in .NET 4.0 will resolve this issue.