Search code examples
c#wcf.net-3.5dictionaryiserializable

Passing recursive collection through WCF


I want to pass a fairly generic set of data through a WCF method. The data is basically just a hierarchical set of key/value pairs, but it's nested to an arbitrary level. I originally considered passing it though as a single string and doing XML or JSON or similar encoding/decoding at either end, but since the WCF transport is XML anyway that seemed a little silly, so I'm hoping there's a way to pass it through "naturally".

The method is fairly straightforward:

[OperationContract]
void ProcessData(DataTree tree);

with:

public class DataTree : Dictionary<string, DataTree>
{
}

This all compiles fine, but when I try to run the service it crashes with a StackOverflowException under DataContract.GetStableName.

I've tried putting a [CollectionDataContract] attribute on the DataTree class and explicitly specifying all the names, but that didn't seem to make any difference.

I've also tried putting a [DataContract] on it, but then it fails even earlier because Dictionary is ISerializable.

Any ideas? Is there a better way to do this?


Solution

  • As it turned out, another requirement came up (specifying a simple value for a node in addition to a list of children), so I ended up defining a child type for it anyway, which appears to have made WCF happy with it:

    [CollectionDataContract(IsReference = true, ItemName = "Param",
                            KeyName = "Name", ValueName = "Data")]
    public class DataTree : Dictionary<string, DataTreeEntry>
    {
    }
    
    [DataContract]
    public class DataTreeEntry
    {
        [DataMember]
        public string Value { get; set; }
    
        [DataMember]
        public DataTree Children { get; set; }
    }