我正在将Hangfire集成到我的项目中。我从BackgroundJob.Enqueue中的Newtonsoft.Json.Serialization得到了一个例外,如下所示。谁能告诉我如何解决这个问题?
为类型为“ASP.global_asax”的属性“ApplicationInstance”检测到自引用循环。 Path'NamingContainer.Page.ModelBindingExecutionContext.HttpContext.ApplicationInstance.Context'。
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CheckForCircularReference(JsonWriter writer,Object value,JsonProperty property,JsonContract contract,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.CalculatePropertyValues(JsonWriter writer,Object value,JsonContainerContract contract,JsonProperty member,JsonProperty property,JsonContract& memberContract,Object& memberValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer,Object value,JsonObjectContract contract,JsonProperty member,JsonContainerContract collectionContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer,Object value,JsonContract valueContract,JsonProperty member,JsonContainerContract containerContract,JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter,Object value,Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter,Object value,Type objectType) 在Newtonsoft.Json.JsonConvert.SerializeObject(对象值,类型类型,格式化格式,JsonSerializerSettings设置) 在Hangfire.Common.Job.GetArguments(MethodCallExpression callExpression) at Hangfire.Common.Job.FromExpression(Expression
1 methodCall) at Hangfire.BackgroundJobClientExtensions.Create(IBackgroundJobClient client, Expression
1 methodCall,IState state) 在Hangfire.BackgroundJobClientExtensions.Enqueue(IBackgroundJobClient客户端,表达式1 methodCall) at Hangfire.BackgroundJob.Enqueue(Expression
1 methodCall)
答案 0 :(得分:0)
您正在使用名为“ApplicationInstance”的属性序列化对象,该属性包含对自身的引用。将此属性标记为不可序列化。
How to serialize as Json an object structure with circular references?可能会有所帮助。
答案 1 :(得分:0)
我发现问题来自一个未序列化的参数。现在我解决了我的问题。一切顺利。谢谢大家。
答案 2 :(得分:0)
我收到此错误(与Hangfire无关),因为我有一个控制器方法:
public async Task<IHttpActionResult> MyAction(...)
我正在返回ActionResult
包裹在另一个ActionResult
中,如下所示:
return Ok(await Get(Id));
...其中Get(Id)
是控制器上的另一个动作。
我将return语句更改为:
return await Get(Id);
它解决了这个问题。错误中提到的ApplicationInstance
属性是我需要的提示:控制器没有从Get
方法序列化我的POCO实体;它正在序列化包含该POCO的IHttpActionResult!
答案 3 :(得分:0)
当我从Fiddler体内调用带有json的Post时收到此错误。 我终于意识到了解决方案。我没有在标题中设置内容类型。一旦我设置Content-Type:application / json; charset = UTF-8,问题就消失了。