我正在尝试创建一个从azure上的sqlserver检索数据的webapi,我收到了这个错误。 我是asp.net webapi的新手这是我的第一次看到教程,我做了他正在做的事情,但我得到了这个错误
堆栈跟踪:
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.DataContractSerializer.WriteObject(XmlWriter writer, Object graph)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)
at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__1b.MoveNext()
我的控制器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DataBaseAccess;
namespace JoSaleWebApi.Controllers
{
public class BrandController : ApiController
{
public IEnumerable<brand_Table> Get()
{
using (JoSaleDbEntities enitites = new JoSaleDbEntities())
{
return enitites.brand_Table.ToList();
}
}
public brand_Table Get(int id)
{
using (JoSaleDbEntities enitites = new JoSaleDbEntities())
{
return enitites.brand_Table.FirstOrDefault(b => b.brand_id == id);
}
}
}}
ADO生成的课程
namespace DataBaseAccess
{
using System;
using System.Collections.Generic;
public partial class brand_Table
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public brand_Table()
{
this.branch_Table = new HashSet<branch_Table>();
this.sale_Table = new HashSet<sale_Table>();
}
public int brand_id { get; set; }
public string brand_name { get; set; }
public string brand_imageUrl { get; set; }
public int company_id { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<branch_Table> branch_Table { get; set; }
public virtual company_Table company_Table { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<sale_Table> sale_Table { get; set; }
}
}
答案 0 :(得分:0)
根据您的堆栈跟踪和代码,我发现您没有为您的实体应用DataContract,并且没有为XmlFormatter提供DataContractResolver。另一个问题是序列化与其他实体模型(company_Table)有关系的实体模型(brand_Table)。在将实体序列化为JOSN或xml时,此关系会导致自引用循环异常。要解决这些问题,可以在数据库上下文的构造方法中将LazyLoadingEnabled和ProxyCreationEnabled属性设置为false。
public JoSaleDbEntities()
: base("name=JoSaleDbEntities ")
{
this.Configuration.LazyLoadingEnabled = false;
this.Configuration.ProxyCreationEnabled = false;
}