开发者

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 ReferencePreservingData开发者_开发百科ContractSerializerOperationBehavior
      :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?


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*/);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜