WCF下传递EF实体对象中出现异常:无法将 EntityReference 对象序列化

近期项目采用了WCF+Entity Framework+WinForm的框架.使用一个Windows Service托管了WCF用于处理后的一些定时任务,并且充当着逻辑层来处理一些与前端的交互.在Winform运行过程中出现异常,如下图:

image

详细信息中:

 

异常详细信息有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。

************** 异常文本 **************
System.InvalidOperationException: 无法将 EntityReference 对象序列化。当 RelationshipManager 属于不实现 IEntityWithRelationships 的实体对象时,无法将此类型的对象序列化。

Server stack trace: 
   在 System.Data.Objects.DataClasses.EntityReference`1.OnSerializing(StreamingContext context)
   在 WriteEntityReferenceOfConnectionMT62RNlBToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   在 System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   在 WriteSystemModuleToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   在 System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   在 WriteArrayOfSystemModuleToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   在 System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   在 WriteConnectionToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   在 System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   在 WriteSystemModuleToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   在 System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   在 System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   在 System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   在 System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   在 System.Runtime.Serialization.XmlObjectSerializer.WriteObject(XmlDictionaryWriter writer, Object graph)
   在 System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameterPart(XmlDictionaryWriter writer, PartInfo part, Object graph)
   在 System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameter(XmlDictionaryWriter writer, PartInfo part, Object graph)
   在 System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeParameters(XmlDictionaryWriter writer, PartInfo[] parts, Object[] parameters)
   在 System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.SerializeBody(XmlDictionaryWriter writer, MessageVersion version, String action, MessageDescription messageDescription, Object returnValue, Object[] parameters, Boolean isRequest)
   在 System.ServiceModel.Dispatcher.OperationFormatter.SerializeBodyContents(XmlDictionaryWriter writer, MessageVersion version, Object[] parameters, Object returnValue, Boolean isRequest)
   在 System.ServiceModel.Dispatcher.OperationFormatter.OperationFormatterMessage.OperationFormatterBodyWriter.OnWriteBodyContents(XmlDictionaryWriter writer)
   在 System.ServiceModel.Channels.BodyWriter.WriteBodyContents(XmlDictionaryWriter writer)
   在 System.ServiceModel.Channels.BodyWriterMessage.OnWriteBodyContents(XmlDictionaryWriter writer)
   在 System.ServiceModel.Channels.Message.OnWriteMessage(XmlDictionaryWriter writer)
   在 System.ServiceModel.Channels.Message.WriteMessage(XmlDictionaryWriter writer)
   在 System.ServiceModel.Channels.BufferedMessageWriter.WriteMessage(Message message, BufferManager bufferManager, Int32 initialOffset, Int32 maxSizeQuota)
   在 System.ServiceModel.Channels.BinaryMessageEncoderFactory.BinaryMessageEncoder.WriteMessage(Message message, Int32 maxMessageSize, BufferManager bufferManager, Int32 messageOffset)
   在 System.ServiceModel.Channels.FramingDuplexSessionChannel.EncodeMessage(Message message)
   在 System.ServiceModel.Channels.FramingDuplexSessionChannel.OnSend(Message message, TimeSpan timeout)
   在 System.ServiceModel.Channels.OutputChannel.Send(Message message, TimeSpan timeout)
   在 System.ServiceModel.Dispatcher.DuplexChannelBinder.Request(Message message, TimeSpan timeout)
   在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   在 YC.Report.Client.ReportData.IReportData.GetDataItemsByDateModule(DateTime Date, SystemModule Module)
   在 YC.Report.Client.ReportData.ReportDataClient.GetDataItemsByDateModule(DateTime Date, SystemModule Module) 位置 D:\YCReport\YC.Report.Client\Service References\ReportData\Reference.cs:行号 2904
   在 YC.Report.Client.MainForm.MainForm_Load(Object sender, EventArgs e) 位置 D:\YCReport\YC.Report.Client\MainForm.cs:行号 265
   在 System.Windows.Forms.Form.OnLoad(EventArgs e)
   在 System.Windows.Forms.Form.OnCreateControl()
   在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   在 System.Windows.Forms.Control.CreateControl()
   在 System.Windows.Forms.Control.WmShowWindow(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   在 System.Windows.Forms.Form.WmShowWindow(Message& m)
   在 System.Windows.Forms.Form.WndProc(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.239 (RTMGDR.030319-2300)
    基本代码: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
YC.Report.Client
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///C:/Program%20Files%20(x86)/Microsoft/ReportClientSetup/YC.Report.Client.exe
----------------------------------------
System.Windows.Forms
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.235 built by: RTMGDR
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.236 built by: RTMGDR
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.ServiceModel
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.233 built by: RTMGDR
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel/v4.0_4.0.0.0__b77a5c561934e089/System.ServiceModel.dll
----------------------------------------
System.Core
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.233 built by: RTMGDR
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Data.Entity
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.Entity/v4.0_4.0.0.0__b77a5c561934e089/System.Data.Entity.dll
----------------------------------------
System.Runtime.Serialization
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Serialization.dll
----------------------------------------
System.Configuration
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.233 built by: RTMGDR
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.DurableInstancing
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.DurableInstancing/v4.0_4.0.0.0__31bf3856ad364e35/System.Runtime.DurableInstancing.dll
----------------------------------------
SMDiagnostics
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/SMDiagnostics/v4.0_4.0.0.0__b77a5c561934e089/SMDiagnostics.dll
----------------------------------------
System.WorkflowServices
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.WorkflowServices/v4.0_4.0.0.0__31bf3856ad364e35/System.WorkflowServices.dll
----------------------------------------
System.ServiceModel.Web
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.233 (RTMGDR.030319-2300)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Web/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Web.dll
----------------------------------------
System.ServiceModel.Discovery
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Discovery/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Discovery.dll
----------------------------------------
System.ServiceModel.Activities
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Activities/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Activities.dll
----------------------------------------
System.ServiceModel.Routing
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Routing/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Routing.dll
----------------------------------------
Microsoft.VisualStudio.Diagnostics.ServiceModelSink
    程序集版本: 4.0.0.0
    Win32 版本: 10.0.30319.1
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualStudio.Diagnostics.ServiceModelSink/v4.0_4.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualStudio.Diagnostics.ServiceModelSink.dll
----------------------------------------
System.ServiceModel.Channels
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.ServiceModel.Channels/v4.0_4.0.0.0__31bf3856ad364e35/System.ServiceModel.Channels.dll
----------------------------------------
System.IdentityModel
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.IdentityModel/v4.0_4.0.0.0__b77a5c561934e089/System.IdentityModel.dll
----------------------------------------
System.Web
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.237 built by: RTMGDR
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Web/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------
System.RunTime.Serialization.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Serialization.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Runtime.Serialization.resources.dll
----------------------------------------
System.Data
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.237 (RTMGDR.030319-2300)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_32/System.Data/v4.0_4.0.0.0__b77a5c561934e089/System.Data.dll
----------------------------------------
Accessibility
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/Accessibility/v4.0_4.0.0.0__b03f5f7f11d50a3a/Accessibility.dll
----------------------------------------
mscorlib.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.235 (RTMGDR.030319-2300)
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
System.Data.Entity.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Data.Entity.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Data.Entity.resources.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如: 

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>

启用 JIT 调试后,任何未经处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。

-------------------------------------------------------------------------------------------------------------------------------------------------

 

 

经过再三调试后我们发现,原因是在我的Winform的项目中增加了System.Data.Entity的引用后造成的.

image

 

下图是我在Winform,也就是WCF客户端中增加System.Data.Entity引用后所生成的DataItem类图

image

这时我在客户端中调用数据实体类时就会出现上述异常.可以看出在当前项目中生成的代理类完全使用了System.Data.Entity.EntityObject作为基类。但是从WCF服务端获取的数据契约类与其有所不同。

 

解决方法:

1、我去掉System.Data.Entity引用后,又重新生成了客户端代理。

image

自动生成的关系类图如下:

 

image

 

 

2、如果一定要在WCF客户端项目中引入System.Data.Entity则需要服务引用设置中将使用引用的程序集中的类型列表中将System.Data.Entity去掉便可。

image

转载于:https://www.cnblogs.com/scally/archive/2011/12/13/2286885.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值