我'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使用我自己的DataContract代理,如下所述:http://timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx . 在这里我设置preserveObjectReferences = true
我的模型包含周期(即客户与IList [订单])
当我从我的服务中检索一个对象时它工作正常,但是当我尝试将同一个对象发送回wcf服务时,我收到错误:
System.ServiceModel.CommunicationException未被用户代码处理Message =尝试序列化参数http://tempuri.org/:searchCriteria时出错 . InnerException消息是'对象图...'包含循环,如果未跟踪引用,则无法序列化 . 考虑使用DataContractAttribute,并将IsReference属性设置为true .
所以循环引用现在是Silverlight中的一个问题,所以我尝试将我的DataContract更改为[DataContract(IsReference = true)]但是现在当我尝试从我的服务中检索一个对象时,我得到以下异常:
System.ExecutionEngineException未处理Message =抛出了类型'System.ExecutionEngineException'的异常 . 的InnerException:
有任何想法吗?
2 回答
您是否在客户端和服务器上使用IsReference = true?
我得到了James Kovacs解决的这个问题,我将在这里发布链接:
One Option
这是更好的解决方案:
要为操作或服务范围启用循环引用,可以使用自定义行为等 . 基本上,您需要能够挂钩到序列化程序实例化过程并使用上面的重载创建实例:
Subclass DataContractSerializerOperationBehavior
Ovverride CreateSerializer方法
创建一个新的DCS实例,将true传递给preserveObjectReferences参数 .
来自Here
这是.Net 3.5中的Silverlight 3和WCF中的一个错误 .
我现在已升级到.Net 4和Silverlight 4,所有内容(循环引用和从WCF返回的接口类型)都可以很好地协同工作!