Search code examples
c#web-servicesentity-framework-4web

Exceptions when using web service with EF


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


Solution

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