首页 文章

检测到属性asp.net web api的自引用循环

提问于
浏览
1

我在访问联系人对象的get方法时收到无限循环错误 . 我正在访问其他项目中广泛使用的数据模型,我认为这不是问题 .

如果有其他信息可以提供帮助,请告知我们 .

我通过url http://localhost:65207/Api/contacts访问getContacts

这是方法

return db.contacts.Include(c => c.businesscontacts);

如果我应用take(1),查询将成功返回

return db.contacts.Take(1).Include(c => c.businesscontacts);

和堆栈跟踪

{“消息”:“发生了错误 . ”,“ExceptionMessage”:“'ObjectContent`1'类型无法序列化内容类型'text / html; charset = utf-8'的响应正文 . ”,“ ExceptionType“:”System.InvalidOperationException“,”StackTrace“:null,”InnerException“:{”Message“:”发生错误 . “,”ExceptionMessage“:”检测到属性'contact'的自引用循环'类型'系统.Data.Entity.DynamicProxies.contact_86EA38F6B924D1435BCF3A7E50D07D29E0003280D2026E3A4AF9F36C87F1F214'.Path'[1] .businesscontacts [0]' . “,”ExceptionType“:”Newtonsoft.Json.JsonSerializationException“,”StackTrace“:”at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter . CheckForCircularReference(JsonWriter writer,Object value,JsonProperty属性,JsonContract Contract ,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,Object value,JsonContainerContract contract,JsonPropert) y成员,JsonProperty属性,JsonContract&memberContract,Object&memberValue)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract)合约,JsonProperty成员,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContr) act,JsonProperty containerProperty)\ r \ n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter .SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer,IEnumerable values,JsonArrayContract contract,JsonProperty member,JsonContainerContract collectionContract) ,JsonProperty containerProperty)\ r \ n在Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty成员,JsonContainerContract containerContract,JsonProperty containerProperty)\ r \ n在New在Newtonsoft.Json的Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType)\ r \ n中的tonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType)\ r \ n . System.Net.Http上的System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)\ r \ n中的JsonSerializer.Serialize(JsonWriter jsonWriter,Object value)\ r \ n . Formating.JsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,Encoding effectiveEncoding)\ r \ n在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type,Object value,Stream writeStream,HttpContent content)\ r \在System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(类型类型,对象值,流writeStream,HttpContent内容,TransportContext transportContext,CancellationToken cancellationToken)\ r \ n ---堆栈跟踪结束抛出异常的前一个位置--- \ r \ n在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\ r \ n在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)\ r \ n at System.Web.Http.WebHost.HttpControllerHandler.d__1b.MoveNext()“}}中的System.Runtime.CompilerServices.TaskAwaiter.GetResult()\ r \ n

1 回答

相关问题