WCF与Nhibernate 对象循环引用问题

导读:
  On a recent project, I was trying to return an object (with child objects) from an ASMX Web Service and was using the XmlSerializer for serialization. (When doing ASMX Web Services, you really have no choice but to use the XmlSerializer.) The child objects contained a circular reference, which ended up being the bane of my existence because the XmlSerializer can't handle circular references. The XmlSerializer reports, "Circular reference detected" and gives up. It just can't serialize it. The amount of code (and changes to the domain model) to work around this limitation was just plain stupid, not to mention the subtle bugs that needed to be fixed when object identity wasn't respected in other areas of the object graph.
  So what does this have to do with Windows Communication Foundation (WCF)? Given how excited I've been about WCF, my fellow developers (and the PM) asked if WCF would have avoided the XmlSerializer problems that we had experienced. My curiosity was piqued and I had to find out. As it turns out, WCF can handle circular references and preserve object identity. Or more correctly, the DataContractSerializer can. This would have saved me untold effort if I had been able to use WCF on the project. (Unfortunately release dates didn't match up. Ironically the project released into production the day before WCF went RTM.) Just a pronouncement that WCF can handle circular references wouldn't make for a very interesting blog entry. The reality is that out-of-the-box, WCF cannot handle circular references even though the DataContractSerializer can. With a bit of digging, I was able to configure WCF to serialize circular references and properly preserve object identity.
  Let's take a deeper look... Consider a simple object graph with a circular reference. The simplest is a parent-child relationship where the child has a back-reference to the parent. (My situation was more complex, but this example suffices.) Here is a Person class, which has a list of Children, as well as a reference to its parent.
  [DataContract(Namespace="http://www.jameskovacs.com/Examples/WcfCircularRefs")]
  publicclassPersonDTO{
  [DataMember]
  publicstringName {
  get{ returnm_name; }
  set{ m_name = value}
  }
  
  privatestringm_name;
  [DataMember(Name="Parent")]
  privatePersonm_parent;
  [DataMember(Name = "Children")]
  privateList m_children = newList ();
  
  /* Additional constructors/properties/methods elided. */
  /* I've also omitted the Order and IsRequired DataMember parameters above. */
  /* Full source can be found in the linked zip file at the end. */
  }
  
  Now why can't WCF handle circular references out-of-the-box. The reason is that there is no industry-accepted, interoperable way of expressing anything but parent-child relationships in XML. You can use the ID/IDREF feature of XML or the key/keyref feature of XML Schema, but a lot of serializers don't respect these attributes or handle them properly. So if you want to serialize circular references, you need to stray out of the realm of safe interoperability. I found some very intriguing information in Aaron Skonnard's MSDN article about the DataContractSerializer, Serialization in Windows Communication Foundation. When instantiating the DataContractSerializer, you can tell it to respect object identity (and thus handle circular references) by passing truefor preserveObjectReferences constructor parameter.
  DataContractSerializerserializer = newDataContractSerializer(typeof(PersonDTO), null, int.MaxValue, false, true/* preserveObjectReferences */, null);
  serializer.WriteObject(memStream, person);
  The resulting XML looks like this:
  
   Sam Spade
  
   Unknown
  
  
  
  
  
   Jane Spade
  
  
  
  
   Bobby Spoon
  
  
  
  
  
  You'll notice that references to other objects are encoded using the z:Id attribute. So the DataContractSerializer can handle circular references, but how do we tell WCF to pass true for the preserveObjectReferences parameter when creating a DataContractSerializer? We can't, but what we can do is do it ourselves and wedge our code into the WCF pipeline. Is that cool or what? All the details can be found in Ingo Rammer's MSDN Library Article, From .NET Remoting to the Windows Communication Foundation (WCF). We create a class derived from DataContractSerializerOperationBehaviour, which we'll call PreserveReferencesOperationBehavior. Here's the important overload:
  publicoverrideXmlObjectSerializerCreateSerializer(
  Typetype, XmlDictionaryStringname, XmlDictionaryStringns,
  IList knownTypes) {
  returnnewDataContractSerializer(type, name, ns, knownTypes,
  0x7FFF,
  false,
  true/* preserveObjectReferences */,
  null);
  }
  So we've wedged in the preserveObjectReferences = true. We then create an attribute, PreserveReferencesAttribute, which implements IOperationBehavior, to inject the PreserveReferencesOperationBehavior.
  publicvoidApplyDispatchBehavior(OperationDescriptiondescription,
  DispatchOperationdispatch) {
  IOperationBehaviorinnerBehavior =
  newPreserveReferencesOperationBehavior(description);
  innerBehavior.ApplyDispatchBehavior(description, dispatch);
  }
  Finally, we apply this attribute to the OperationContract in our ServiceContract.
  [ServiceContract(Namespace="http://www.jameskovacs.com/Examples/WcfCircularRefs")]
  publicinterfaceIPersonService{
  [OperationContract]
  [PreserveReferences]
  PersonDTOGetDefaultPerson();
  }
  Our service is now able to serialize circular references, as well as preserve object identity. If we need to transmit objects with circular references back to the service, we could make similar changes to inject PreserveReferencesOperationBehavior into the client-side proxy.
  To summarize, WCF is a very extensible framework for distributed communication. We were able to make some small modifications to the WCF pipeline that allowed us to return an object graph with circular references from a service. With the release of WCF, the life of the distributed application developer just got a whole lot easier. Full source code can be found here (16 KB).

本文转自
http://www.jameskovacs.com/blog/CommentView.aspx?guid=477b077c-e65e-4547-8289-4e1bc17b3de7
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值