serializing custom objects
-
28-05-2021 - |
質問
I have 2 classes that derive from a generic base class.
[Serializable()]
[XmlInclude(typeof(User))]
[XmlInclude(typeof(File))]
public class BaseEntity<T>
{
private long id;
[XmlAttribute(AttributeName = "id")]
public virtual long ID
{
get { return this.id; }
set { this.id = value; }
}
}
[Serializable()]
public class User : BaseEntity<User>
{
private string userName;
private string sharedDirectory;
private bool connected;
[XmlAttribute(AttributeName = "UserName")]
public virtual string UserName
{
get { return this.userName; }
set { this.userName = value; }
}
[XmlAttribute(AttributeName = "SharedDirectory")]
public virtual string SharedDirectory
{
get { return this.sharedDirectory; }
set { this.sharedDirectory = value; }
}
[XmlAttribute(AttributeName = "IsConnected")]
public virtual bool IsConnected
{
get { return this.connected; }
set { this.connected = value; }
}
}
[Serializable()]
public class File : BaseEntity<File>
{
private string name;
private User user;
[XmlAttribute(AttributeName = "Name")]
public virtual string Name
{
get { return this.name; }
set { this.name = value; }
}
[XmlElement(ElementName = "User", Type = typeof(User))]
public virtual User User
{
get { return this.user; }
set { this.user = value; }
}
}
I have a web service (not WCF service) that return a List of Files (List). When i call the service, the method is preformed but the serializing process fails. This is the Exception I get:
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 ---
Any one knows why?
解決
Its because that i used lazy=true and when i serialized my objects, the serializer tried to serialize the Proxy object which is NOT allowed. when i changed the mapping to lazy=false it worked!