I have 2 applications (WPF and ASP.NET) that use the same entities so i also have a class library that holds the definitions of my entities. I have 2 main objects "User" and "File" (they both derive from a generic class "BaseEntity<T>". The "User" object
has only string and boolean members but the "File" object has "User' type member. I Have a web service and in it i have a function that returns a list of Files (List<File>). My WPF application consumes the web service and calls that same method. when i debug,
the method rens without errors but it seems that an exception is thrown when trying to serialize the return statment. Here is the exception:
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 Castle.Proxies.UserProxy was not expected.
Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_User(String n, String ns, User o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_File(String
n, String ns, File o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_GetFilesByNameResponse(Object[] p)\n at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(Object
objectToSerialize, XmlSerializationWriter writer)\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\n --- End of inner exception stack trace ---\n
at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\n at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)\n
at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)\n at System.Web.Services.Protocols.WebServiceHandler.Invoke()\n --- End of inner exception stack trace ---
I should say that i have added the [XmlInclude(typeof(User))] attribute and the [SoapInclude(typeof(User))] above the base class definition (in the class library) but i still get the same exception.
Since this is a serialization issue, you can try first creating a simplied repro webmethod which just return a single class object(or an array of the certain custom type instances) so as to determine the exact types that might cause the exception.
Also, from the exception message, I found that it indicates the following type information:
=============== System.InvalidOperationException: The type Castle.Proxies.UserProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_User(String
n, String ns, User o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_File(String n, String ns, File o, Boolean isNullable, Boolean needType)\n at
================
So the error is raised when serializing the "User" instance inside the "File" instance. And we can generate some test webmethod focusing on these two types. BTW, is the "Castle.Proxies.UserProxy" type mentioned your custom type? If so, you can also check
on it.
When i saw your resopnce, i suddenly knew what the problem was. I am using NHibernate as my ORM. When i retrieved the "File" object from the DB, NHibernate populated the member User with a proxy and did not full populate it. When i defined the field as Lasy=false
then it was fully populated and the type was indeed my User type and not a proxy.
I should have seen this before! :)
Thanks.
custom typeserializeweb service
Marked as answer by gil.tankus on Apr 04, 2012 06:19 AM
gil.tankus
Member
143 Points
53 Posts
Cutom typed objects and serialization (Through web service)
Apr 02, 2012 04:55 PM|LINK
Hello,
I have 2 applications (WPF and ASP.NET) that use the same entities so i also have a class library that holds the definitions of my entities. I have 2 main objects "User" and "File" (they both derive from a generic class "BaseEntity<T>". The "User" object has only string and boolean members but the "File" object has "User' type member. I Have a web service and in it i have a function that returns a list of Files (List<File>). My WPF application consumes the web service and calls that same method. when i debug, the method rens without errors but it seems that an exception is thrown when trying to serialize the return statment. Here is the exception:
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 Castle.Proxies.UserProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_User(String n, String ns, User o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_File(String n, String ns, File o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_GetFilesByNameResponse(Object[] p)\n at Microsoft.Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(Object objectToSerialize, XmlSerializationWriter writer)\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\n --- End of inner exception stack trace ---\n at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)\n at System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)\n at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)\n at System.Web.Services.Protocols.WebServiceHandler.Invoke()\n --- End of inner exception stack trace ---
I should say that i have added the [XmlInclude(typeof(User))] attribute and the [SoapInclude(typeof(User))] above the base class definition (in the class library) but i still get the same exception.
Why is this?
Thanks :)
custom type serialize web service
kushalrdalal
Contributor
7198 Points
1287 Posts
Re: Cutom typed objects and serialization (Through web service)
Apr 02, 2012 05:31 PM|LINK
Is your service WCF or old asmx web service?
Try this instead of Soapinclude
[KnownType(typeof(User))]
custom type serialize web service
My Blog
LinkedIn Profile
gil.tankus
Member
143 Points
53 Posts
Re: Cutom typed objects and serialization (Through web service)
Apr 02, 2012 06:08 PM|LINK
My service is an regular asmx web service (NOT WCF).
I can't add the KnownType attribute because it doesn't recognize it. what namespace is it? do i need to add a reference?
custom type serialize web service
kushalrdalal
Contributor
7198 Points
1287 Posts
Re: Cutom typed objects and serialization (Through web service)
Apr 02, 2012 07:26 PM|LINK
using System.Runtime.Serialization;
custom type serialize web service
My Blog
LinkedIn Profile
gil.tankus
Member
143 Points
53 Posts
Re: Cutom typed objects and serialization (Through web service)
Apr 02, 2012 07:30 PM|LINK
this is wierd, the compiler still doesn't recognize it. Any other suggestions?
custom type serialize web service
Steven Cheng...
Contributor
4219 Points
548 Posts
Microsoft
Moderator
Re: Cutom typed objects and serialization (Through web service)
Apr 04, 2012 02:44 AM|LINK
Hi gil.tankus,
Since this is a serialization issue, you can try first creating a simplied repro webmethod which just return a single class object(or an array of the certain custom type instances) so as to determine the exact types that might cause the exception.
Also, from the exception message, I found that it indicates the following type information:
===============
System.InvalidOperationException: The type Castle.Proxies.UserProxy was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically.\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_User(String n, String ns, User o, Boolean isNullable, Boolean needType)\n at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_File(String n, String ns, File o, Boolean isNullable, Boolean needType)\n at
================
So the error is raised when serializing the "User" instance inside the "File" instance. And we can generate some test webmethod focusing on these two types. BTW, is the "Castle.Proxies.UserProxy" type mentioned your custom type? If so, you can also check on it.
custom type serialize web service
Feedback to us
Microsoft One Code Framework
gil.tankus
Member
143 Points
53 Posts
Re: Cutom typed objects and serialization (Through web service)
Apr 04, 2012 06:19 AM|LINK
Hi Steven,
When i saw your resopnce, i suddenly knew what the problem was. I am using NHibernate as my ORM. When i retrieved the "File" object from the DB, NHibernate populated the member User with a proxy and did not full populate it. When i defined the field as Lasy=false then it was fully populated and the type was indeed my User type and not a proxy.
I should have seen this before! :)
Thanks.
custom type serialize web service