序列化代码示例中的无限循环

时间:2010-11-24 10:59:21

标签: c# wcf recursion circular-reference

查看here中的以下代码 它是关于在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*/);
        }
    }

不是CreateDataContractSerializer生成无限循环(stackoverflow) - 因此也是前面的CreateSerializer方法吗?

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

现在可能没有使用这些方法?我在这里缺少什么?

1 个答案:

答案 0 :(得分:3)

确实如此。它工作的事实表明当前只调用了最后一个重载。由于涉及不同的参数,也许最好丢失静态方法(这没有帮助):

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