Silverlight WCF serialization DataContract(IsReference=true) problem
I'm have a Silverlight 3 UI that access WCF services which in turn access respositories that use NHibernate. To overcome some NHibernate lazy loading issues with WCF I'm using my own DataContract surrogate as described here: http://timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx. In here I'm setting preserveObjectReferences = true
My model contains cycles (i.e. Customer with IList[Order])
When I retrieve an object from my service it works fine, however when I try and send that same object back to the wcf service I get the error:
System.ServiceModel.CommunicationException was unhandled by user code Message=There was an error while trying to serialize parameter http://tempuri.org/:searchCriteria. The InnerException message was 'Object graph ...' contains cycles and cannot be serialized if references are not tracked. Consider using the DataContractAttribute with the IsReference property set to true.'
So cyclical references are now a problem in Silverlight, so I try change my DataContract to be [DataContract(IsReference=true)] but now when I try to retrieve an object from my service I get the following exception:
System.ExecutionEngineException was unhandled Message=Exception of type 'System.ExecutionEngineException' was thrown. InnerE开发者_开发问答xception:
Any ideas?
This was a bug in both Silverlight 3 and WCF in .Net 3.5.
I've now upgraded to .Net 4 and Silverlight 4 and everything (cyclic references and returning interface types from WCF) is playing nicely together!
Did you use the IsReference=true on both client and server ?
I got this problem solved by James Kovacs, I'll post the link here:
One Option
And this is the better solution:
To enable circular references for operation or service scope, you can use custom behaviors etc. Essentially you need the ability to hook into serializer instantiation process and create the instance using above overload:
Subclass DataContractSerializerOperationBehavior
Ovverride CreateSerializer method
Create a new DCS instance passing true to preserveObjectReferences param.
from Here
精彩评论