Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Endless loop in a code sample on serialization

Have a look at the following code from here.
It's about preserving circular references in a datacontract (object model, object graph, domain model) when serializing in wcf.

class ReferencePreservingDataContractSerializerOperationBehavior
      :DataContractSerializerOperationBehavior
    {
        public ReferencePreservingDataContractSerializerOperationBehavior(
          OperationDescription operationDescription)
          : base(operationDescription) { }

        public override XmlObjectSerializer CreateSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }
 
        private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }
 
        public override XmlObjectSerializer CreateSerializer(
          Type type, XmlDictionaryString name, XmlDictionaryString ns,
          IList<Type> knownTypes)
        {
            return new DataContractSerializer(type, name, ns, knownTypes,
                0x7FFF /*maxItemsInObjectGraph*/,
                false/*ignoreExtensionDataObject*/,
                true/*preserveObjectReferences*/,
                null/*dataContractSurrogate*/);
        }
    }

Isn't CreateDataContractSerializer generating an endless loop (stackoverflow) - and therefore also the preceding CreateSerializer method?

private static XmlObjectSerializer CreateDataContractSerializer(
          Type type, string name, string ns, IList<Type> knownTypes)
        {
            return CreateDataContractSerializer(type, name, ns, knownTypes);
        }

Now maybe these methods are not in use? What am I missing here?

like image 850
Gerard Avatar asked Nov 24 '10 10:11

Gerard


1 Answers

It indeed appears so. The fact that it works suggests that only the last overload is currently being invoked. Since there are different parameters involved, perhaps it would be better to lose the static method (that isn't helping):

public override XmlObjectSerializer CreateSerializer(
  Type type, string name, string ns, IList<Type> knownTypes)
{
    return new DataContractSerializer(type, name, ns, knownTypes,
        0x7FFF /*maxItemsInObjectGraph*/,
        false/*ignoreExtensionDataObject*/,
        true/*preserveObjectReferences*/,
        null/*dataContractSurrogate*/);
}

public override XmlObjectSerializer CreateSerializer(
  Type type, XmlDictionaryString name, XmlDictionaryString ns,
  IList<Type> knownTypes)
{
    return new DataContractSerializer(type, name, ns, knownTypes,
        0x7FFF /*maxItemsInObjectGraph*/,
        false/*ignoreExtensionDataObject*/,
        true/*preserveObjectReferences*/,
        null/*dataContractSurrogate*/);
}
like image 123
Marc Gravell Avatar answered Oct 07 '22 12:10

Marc Gravell