Asp.net mvc 使用MVC4 Web API的Nhibernate中的XML序列化错误

Asp.net mvc 使用MVC4 Web API的Nhibernate中的XML序列化错误,asp.net-mvc,nhibernate,asp.net-web-api,Asp.net Mvc,Nhibernate,Asp.net Web Api,您好,我们正在使用NHibernate在MVC4中创建一个web API。但是当我们调用web API时,它会抛出错误。这里是错误 <Error><Message>An error has occurred.</Message><ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application

您好,我们正在使用NHibernate在MVC4中创建一个web API。但是当我们调用web API时,它会抛出错误。这里是错误

<Error><Message>An error has occurred.</Message><ExceptionMessage>The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace/><InnerException><Message>An error has occurred.</Message><ExceptionMessage>Type 'NHibernate.Collection.Generic.PersistentGenericSet`1[[MvcAngular.Models.BankAccount, MvcAngular, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ArrayOfBankAccount:http://schemas.datacontract.org/2004/07/MvcAngular.Models' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.</ExceptionMessage><ExceptionType>System.Runtime.Serialization.SerializationException</ExceptionType><StackTrace>   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle objectTypeHandle, Type objectType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteCustomerInfoToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , ClassDataContract )
   at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialize(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializeReference(XmlWriterDelegator xmlWriter, Object obj, Boolean isDeclaredType, Boolean writeXsiType, Int32 declaredTypeID, RuntimeTypeHandle declaredTypeHandle)
   at WriteArrayOfCustomerInfoToXml(XmlWriterDelegator , Object , XmlObjectSerializerWriteContext , CollectionDataContract )
   at System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter, Object obj, XmlObjectSerializerWriteContext context)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle declaredTypeHandle)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeAndVerifyType(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, Boolean verifyKnownType, RuntimeTypeHandle declaredTypeHandle, Type declaredType)
   at System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiTypeAtTopLevel(DataContract dataContract, XmlWriterDelegator xmlWriter, Object obj, RuntimeTypeHandle originalDeclaredTypeHandle, Type graphType)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph, DataContractResolver dataContractResolver)
   at System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator writer, Object graph)
   at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
   at System.Net.Http.Formatting.XmlMediaTypeFormatter.<>c__DisplayClass7.<WriteToStreamAsync>b__6()
   at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)</StackTrace></InnerException></Error>
发生错误。'ObjectContent'1'类型未能序列化内容类型'application/xml的响应正文;charset=utf-8'.System.InvalidOperationException发生错误。使用数据协定名称“ArrayOfBankAccount:http://schemas.datacontract.org/2004/07/MvcAngular.Models”他说。考虑使用DATACONTRORTCORDEVER或将未知类型的任何类型添加到已知类型的列表中,例如,通过使用KnownTypeAttribute属性或将它们添加到传递给DataContractSerializer.System.Runtime.Serialization.Serialization的已知类型列表中,在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializationAndVerifyType中出现SerializationException(DataContract DataContract、XmlWriterDelegator xmlWriter、对象obj、布尔verifyKnownType、运行时类型句柄declaredTypeHandle、类型declaredType) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithXsiType(XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle objectTypeHandle,Type objectType,Int32 declaredTypeID,RuntimeTypeHandle declaredTypeHandle,Type declaredType) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialization(XmlWriterDelegator xmlWriter、Object obj、Boolean isDeclaredType、Boolean writeXsiType、Int32 declaredTypeID、RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializationReference(XmlWriterDelegator xmlWriter、Object obj、Boolean isDeclaredType、Boolean writeXsiType、Int32 declaredTypeID、RuntimeTypeHandle declaredTypeHandle) 在WriteCustomerInfoToXml(XmlWriterDelegator、Object、XmlObjectSerializerWriteContext、ClassDataContract) at System.Runtime.Serialization.ClassDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter、Object obj、XmlObjectSerializerWriteContext上下文) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract DataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializeWithoutXsiType(DataContract DataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerialization(XmlWriterDelegator xmlWriter、Object obj、Boolean isDeclaredType、Boolean writeXsiType、Int32 declaredTypeID、RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.InternalSerializationReference(XmlWriterDelegator xmlWriter、Object obj、Boolean isDeclaredType、Boolean writeXsiType、Int32 declaredTypeID、RuntimeTypeHandle declaredTypeHandle) 在客户InfoToXML(XmlWriterDelegator、Object、XmlObjectSerializerWriteContext、CollectionDataContract)的WriteArayOfCustomerInfoToXML(XmlWriterDelegator、Object、XmlObjectSerializerWriteContext) 在System.Runtime.Serialization.CollectionDataContract.WriteXmlValue(XmlWriterDelegator xmlWriter、对象obj、XmlObjectSerializerWriteContext上下文) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.WriteDataContractValue(DataContract DataContract,XmlWriterDelegator xmlWriter,Object obj,RuntimeTypeHandle declaredTypeHandle) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializationAndVerifyType(DataContract、XmlWriterDelegator、xmlWriter、Object obj、布尔verifyKnownType、RuntimeTypeHandle declaredTypeHandle、Type declaredType) 在System.Runtime.Serialization.XmlObjectSerializerWriteContext.SerializationWithXSITypeAttoplevel(DataContract-DataContract、XmlWriterDelegator-xmlWriter、Object obj、RuntimeTypeHandle-OriginaldRecaredTypeHandle、Type graphType) 在System.Runtime.Serialization.DataContractSerializer.InternalWriteObjectContent(XmlWriterDelegator编写器、对象图、DataContractResolver DataContractResolver) 在System.Runtime.Serialization.DataContractSerializer.InternalWriteObject(XmlWriterDelegator编写器、对象图、DataContractResolver DataContractResolver) 在System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator编写器、对象图、DataContractResolver DataContractResolver) 在System.Runtime.Serialization.XmlObjectSerializer.WriteObjectHandleExceptions(XmlWriterDelegator编写器,对象图) at System.Runtime.Serialization.DataContractSerializer.WriteObject(XmlWriter编写器,对象图) 在System.Net.Http.Formatting.XmlMediaTypeFormatter.c__DisplayClass7.b__u6()中 在System.Threading.Tasks.TaskHelpers.RunSynchronously(操作操作,取消令牌) 我们正在WebApi中成功地获得结果。这是类的模式。我们在父类中使用了ISET int

public class CustomerInfo
    {

        int _CustomerId;
        public virtual int CustomerId
        {
            get { return _CustomerId; }
            set { _CustomerId = value; }
        }

        string _Name;
        [Required]
        public virtual string Name
        {
            get { return _Name; }
            set { _Name = value; }
        }

        public virtual ISet<BankAccount> BankAccounts { get; set; }


        public virtual void SetBankAccount(ISet<BankAccount> bankact)
        {
            this.BankAccounts = bankact;
        }


        public virtual ISet<BankAccount> GetBankAccounts()
        {
            return this.BankAccounts;
        }

    }
公共类CustomerInfo
{
int_客户ID;
公共虚拟int客户ID
{
获取{return\u CustomerId;}
设置{u CustomerId=value;}
}
字符串\u名称;
[必需]
公共虚拟字符串名
{
获取{return\u Name;}
设置{u Name=value;}
}
公共虚拟ISet银行帐户{get;set;}
公共虚拟银行账户(ISet bankact)
{
this.BankAccounts=bankact;
}
公共虚拟ISet GetBankAccounts()
{
归还此银行账户;
}
}
这就是webapi的Get方法

 public IList<CustomerInfo> Get()
        {
            CustomerInfoDAL cd = new CustomerInfoDAL();
            var lst = cd.GetCustomer();
            return lst;
        }
public IList Get()
{
CustomerInfoDAL cd=新CustomerInfoDAL();
var lst=cd.GetCustomer();
返回lst;
}
我们正在成功地获得lst的结果。但是