【问题标题】:Endless loop in a code sample on serialization序列化代码示例中的无限循环
【发布时间】:2010-11-24 10:59:21
【问题描述】:

看看下面来自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);
        }

现在也许这些方法没有被使用?我在这里错过了什么?

【问题讨论】:

    标签: c# wcf recursion circular-reference


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-08-23
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多