我有两个派生自通用基类的类。
[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; }
}
}
我有一个返回文件列表(列表)的Web服务(不是WCF服务)。当我调用该服务时,该方法已执行但序列化过程失败。这是我得到的例外情况:
System.Web.Services.Protocols.SoapException:服务器无法处理请求。 ---&GT; System.InvalidOperationException:生成XML文档时出错。 ---&GT; System.InvalidOperationException:不期望Castle.Proxies.UserProxy类型。使用XmlInclude或SoapInclude属性指定静态未知的类型。\ n在Microsoft的Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write4_User(String n,String ns,User o,Boolean isNullable,Boolean needType)\ n。在Microsoft的Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write7_GetFilesByNameResponse(Object [] p)\ n的Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write6_File(String n,String ns,File o,Boolean isNullable,Boolean needType)\ n。 System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,Object o,XmlSerializerNamespaces namespaces,String encodingStyle,String id)中的Xml.Serialization.GeneratedAssembly.ArrayOfObjectSerializer1.Serialize(Object objectToSerialize,XmlSerializationWriter writer)\ n System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter,Object o,XmlSerializerNamespaces namespaces,String)中内部异常堆栈跟踪--- \ n在System.Web.Services.Protocols.SoapServerProtocol.WriteReturns(Object [] returnValues,Stream outputStream)\ n中的encodingStyle,String id)\ n在System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object [] returnValues)\ n在System.Web.Services.Protocols.WebServiceHandler.Invoke()\ n ---内部异常堆栈跟踪结束---
谁知道为什么?
答案 0 :(得分:1)
因为我使用了lazy = true,当我序列化我的对象时,序列化程序试图序列化不允许的Proxy对象。当我将映射更改为lazy = false时,它工作正常!