2010-04-21 100 views
1

我有一個Silverlight 3 UI訪問WCF服務,後者又訪問使用NHibernate的存儲庫。爲了克服一些NHibernate與WCF的延遲加載問題,我使用我自己的DataContract代理,如下所述:http://timvasil.com/blog14/post/2008/02/WCF-serialization-with-NHibernate.aspx。在這裏我設置preserveObjectReferences =真Silverlight WCF序列化DataContract(IsReference = true)問題

我的模型包含週期(即客戶用的IList [訂購])

當我取回從我服務的對象,它工作正常,但是當我嘗試發送相同對象回到WCF服務,我得到的錯誤:

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.'

所以循環引用現在在Silverlight中的一個問題,所以我嘗試改變我的DataContract是[DataContract(IsReference =真)],但現在當我嘗試從我的服務檢索一個對象我收到以下異常:

System.ExecutionEngineException was unhandled Message=Exception of type 'System.ExecutionEngineException' was thrown. InnerException:

任何想法?

回答

1

這是一個錯誤在Silverlight 3和WCF都在.net 3.5。

我現在已經升級到.Net 4和Silverlight 4,並且所有東西(循環引用和從WCF返回的接口類型)都很好地一起玩!

0

您是否在客戶端和服務器上都使用IsReference = true?

我得到了這個問題,由詹姆斯·科瓦奇解決,我將張貼在這裏的鏈接:

One Option

這是更好的解決方案:

要啓用操作或服務範圍循環引用,您可以使用自定義行爲等。基本上,您需要能夠掛接到串行器實例化過程並使用以上過載創建實例:

  1. 子類DataContractSerializerOperationBehavior

  2. Ovverride CreateSerializer方法

  3. 創建一個新的DCS實例傳遞真實preserveObjectReferences PARAM。

Here

+0

我的數據合同是在客戶端和服務器之間共享的,所以是兩個都有IsReference = true。 我已經在服務器端應用自定義行爲並設置preserveObjectReferences = true。 但是在Silverlight 3中,不能設置preserveObjectReferences = true(沒有構造函數重載並且有一個私有setter)。 它可能與在.net 4.0中修復的此錯誤(http://connect.microsoft.com/VisualStudio/feedback/details/391260/executionengineexception-thrown-from-datacontractserializer-with-ilist-t)有關,但是我不確定是否可以升級到SL 4,但是依賴關係 – 2010-04-21 11:34:04

+0

注意:從wcf服務獲取對象時,不會發生此錯誤,它會在將先前檢索到的對象發送回Silverlight客戶端的服務時發生 – 2010-04-21 11:39:13