将 Web 服务与 EF 一起使用时的异常
我在调用我的网络服务时遇到此异常。我在我的服务中使用 EF 并尝试返回由 EF 创建的 POCO 类。有什么想法吗?
System.Web.Services.Protocols.SoapException:服务器无法 处理请求。 ---> System.InvalidOperationException:有 生成 XML 文档时出错。 ---> System.InvalidOperationException:类型 System.Data.Entity.DynamicProxies.Job_3C04107CB03F22888B19EF7537A795C816D03441852DC37A42729E7542384BBE 没想到。使用 XmlInclude 或 SoapInclude 属性 指定静态未知的类型。在 Microsoft.Xml.Serialization.GenerateAssembly.XmlSerializationWriter1.Write2_Job(字符串 n、字符串 ns、作业 o、布尔值 isNullable、布尔值 needType) at Microsoft.Xml.Serialization.GenerateAssembly.XmlSerializationWriter1.Write9_GetJobByNumberResponse(对象[] p) 在 Microsoft.Xml.Serialization.GenerateAssembly.ArrayOfObjectSerializer13.Serialize(对象 objectToSerialize,XmlSerializationWriter 编写器)位于 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, 对象 o、XmlSerializerNamespaces 命名空间、字符串编码样式、 String id) --- 内部异常堆栈跟踪结束 --- at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, 对象 o、XmlSerializerNamespaces 命名空间、字符串编码样式、 字符串 ID)位于 System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(对象[] returnValues,流输出流)在 System.Web.Services.Protocols.WebServiceHandler.WriteReturns(对象[] 返回值)在 System.Web.Services.Protocols.WebServiceHandler.Invoke() --- 结束 内部异常堆栈跟踪 ---
好吧,我发现一篇文章提到将 ProxyCreationEnabled 属性设置为 false;我已经这样做了,现在效果很好。将此属性设置为 false 会产生什么连锁反应
I'm getting this exception when calling my web service. I'm using EF in my service and trying to return a POCO class created by EF. Any ideas??
System.Web.Services.Protocols.SoapException: Server was unable to
process request. ---> System.InvalidOperationException: There
was an error generating the XML document. --->
System.InvalidOperationException: The type
System.Data.Entity.DynamicProxies.Job_3C04107CB03F22888B19EF7537A795C816D03441852DC37A42729E7542384BBE
was not expected. Use the XmlInclude or SoapInclude attribute to
specify types that are not known statically. at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write2_Job(String
n, String ns, Job o, Boolean isNullable, Boolean needType) at
Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write9_GetJobByNumberResponse(Object[]
p) at
Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer13.Serialize(Object
objectToSerialize, XmlSerializationWriter writer) at
System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle,
String id) --- End of inner exception stack trace --- at
System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,
Object o, XmlSerializerNamespaces namespaces, String encodingStyle,
String id) at
System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[]
returnValues, Stream outputStream) at
System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[]
returnValues) at
System.Web.Services.Protocols.WebServiceHandler.Invoke() --- End of
inner exception stack trace ---
ok i found a post that mentioned setting the ProxyCreationEnabled property to false; Ive done this and now it works fine. Whats the knock on effect of setting this property to false
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在 EF 上下文上禁用延迟加载和代理创建,否则您的 POCO 作为异常状态实际上是一个无法序列化的
DynamicProxy
。本文(存档此处)将提供更多信息。
You need to disable lazy loading and proxy creation on the EF context otherwise your POCO as the exception states is actually a
DynamicProxy
which cannot be serialised.This article (archived here) will provide a little more info.