WebApi项目中,配置了EntityFramework,一顿LINQ操作后接口调试出现以下错误:
{ "Message": "出现错误。", "ExceptionMessage": "“ObjectContent`1”类型未能序列化内容类型“application/json; charset=utf-8”的响应正文。", "ExceptionType": "System.InvalidOperationException", "StackTrace": null, "InnerException": { "Message": "出现错误。", "ExceptionMessage": "Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.TimerServices_HoldDe_5887B15F712A690D23D5730B5D23D92E1914B018B8A19561126379653006AF93'. Path '[0].TimerServices_Base.TimerServices_HoldDetail'.", "ExceptionType": "Newtonsoft.Json.JsonSerializationException", "StackTrace": " 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer, Object value, JsonProperty property, JsonContract contract, 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 member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 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 member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 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 member, 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 member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n 在 Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n 在 Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n 在 System.Net.Http.Formatting.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\n 在 System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- 引发异常的上一位置中堆栈跟踪的末尾 ---\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n 在 System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__22.MoveNext()" } }
开始单看下面这句,以为是WebApi不支持json类型的返回
ObjectContent`1”类型未能序列化内容类型“application/json; charset=utf-8”的响应正文。
参考了:https://www.cnblogs.com/zoujinhua/p/10772321.html
尝试在Register方法里添加配置:
public static void Register(HttpConfiguration config) { // Web API 配置和服务 // 添加这句: GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear(); // Web API 路由 config.MapHttpAttributeRoutes();
结果当然是——没有用。
回到异常重新研究,才发现重点其实是在这一句!
Self referencing loop detected with type 'System.Data.Entity.DynamicProxies.TimerServices_HoldDe_5887B15F712A690D23D5730B5D23D92E1914B018B8A19561126379653006AF93'. Path '[0].TimerServices_Base.TimerServices_HoldDetail'.",
TimerServices_Base.TimerServices_HoldDetail(EF定义的导航属性),出现了循环自引用。
因为A里有B的导航属性,B里也有A,互相引用来来回回无穷尽也……
于是参考大佬的方法:https://www.cnblogs.com/songjl/p/9603830.html
做如下处理:
找到App_Start下的WebApiConfig.cs文件(这里用到的是.Net Framework4.8,其他版本结构可能不同)
还是在注册方法里加一句配置代码:
public static void Register(HttpConfiguration config) { // Web API 配置和服务 // 添加这句: config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; // Web API 路由 config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); }
问题解决!
标签:JsonProperty,Newtonsoft,JsonWriter,EF,value,Object,Json,报错,序列化 From: https://www.cnblogs.com/Yan3399/p/16812498.html