views:

368

answers:

2

Hi,

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. InnerException:

Any ideas?

A: 

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:

  1. Subclass DataContractSerializerOperationBehavior

  2. Ovverride CreateSerializer method

  3. Create a new DCS instance passing true to preserveObjectReferences param.

from Here

Dani
Ciaran
Note: this error is not occurring when getting the object from the wcf service, it occurs when sending a previously retrieved object back over the service from the silverlight client
Ciaran
A: 

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!

Ciaran