【问题标题】:A possible object cycle was detected. in both System.Text.Json and Newtonsoft.Json检测到可能的对象循环。在 System.Text.Json 和 Newtonsoft.Json
【发布时间】:2021-02-24 00:44:24
【问题描述】:

我一直在用这个拔头发。

我有一个非常简单的测试类会抛出这个错误:

失败:Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1] 执行请求时发生未处理的异常。 System.Text.Json.JsonException:检测到可能的对象循环。这可能是由于循环或对象深度大于允许的最大深度 32。考虑在 JsonSerializerOptions 上使用 ReferenceHandler.Preserve 以支持循环。

似乎没有太大的中断,因为put请求成功了,序列化也成功了。

编辑

如果序列化异常确实存在,我已经将其排除在外。我开始认为这是键入 HttpClient 的问题。它会在控制台和 Postman 的响应中抛出异常。但是,它不允许我在代码中捕获异常并且 PUT 调用有效。因此异常发生在 PUT 请求之后,并在将控制权返回给我的应用程序之前得到处理。

我将尝试使用标准的 HttpClientFactor 而不是类型化的客户端,看看是否可行。我知道 JSON 异常是一个红鲱鱼,但它很丑陋并且破坏了响应。

欢迎提出任何建议。

public virtual async Task<CouchResponse> Create(string id, string db, TObj info)
{
  CouchResponse ret = new() { Reason = "Unknown and unExpected error", Ok = false };
  HttpResponseMessage rc = null;

  if (id is null)
  {
    return new CouchResponse() { Id = "missing", Ok = false, Rev = "missing" };
  }
  string url = $"{db}/1";
  try
  {
    // login to Couchdb servwer
    await CouchLogin();

    try
    {

      //var jsonInfo = JsonUtils.Serialize<TestJson>(jTest);
      var jsonInfo = JsonSerializer.Serialize<TObj>(info, options);
      HttpContent content = new StringContent(jsonInfo, Encoding.UTF8, 
        "application/json");
      rc = await client.PutAsync(url, content);
    } 
    catch (Exception eNewton)
    {
      Console.WriteLine($"Json Exception: {eNewton.Message}");
    }

    if (rc is not null)
    {
      var str = await rc.Content.ReadAsStringAsync();
      var ret = JsonSerializer.Deserialize<CouchResponse>(str,options);
      rc.EnsureSuccessStatusCode();
    }

    return ret;
  }
  catch (Exception e)
  {
    Console.WriteLine(e.Message);
    //return ret;

  }
  return ret;
}

建议?

【问题讨论】:

  • 我做到了。我更新了课程,然后尝试对其进行序列化,但出现错误。
  • var jsonInfo = JsonConvert.SerializeObject(jTest,jsonSerializerSettings); 不会抛出异常,请参阅dotnetfiddle.net/Ko6Gww。你能分享一个minimal reproducible example吗?您的ClientBase&lt;T&gt; 类无法编译,因为IDbAccess&lt;&gt;CouchResponseCouchLogin 未定义,请参阅dotnetfiddle.net/O2MMlP。如果没有可编译和可运行的minimal reproducible example,我们不太可能提供帮助。只需共享异常的完整 ToString() 输出,包括异常类型、消息、回溯和内部异常(如果有的话)。
  • 这是一个疯狂的错误。出于某种原因 httpClient.SendAsync 抛出 JSON 错误。但是,它不会冒泡到应用程序,而是在响应消息中返回堆栈跟踪。我将继续使用它,但可能会在 SendAsync 上提出一个问题。

标签: asp.net-core json.net .net-5


【解决方案1】:

多么疯狂的错误。诊断非常缺少领先。我在 create 方法中所做的一切都是正确的。

当我调用 create 方法时缺少的是等待。这使得 sendAsync 似乎遇到了问题,而实际上是控制器试图将任务返回格式化为响应。这导致了响应消息中的堆栈跟踪。感谢大家的帮助。

【讨论】:

    【解决方案2】:

    改变这个

        var jsonSerializerSettings = new JsonSerializerSettings
        {
          ContractResolver = new CamelCasePropertyNamesContractResolver(),
          NullValueHandling = NullValueHandling.Ignore
        };
    

    到这里

            var jsonSerializerSettings = new JsonSerializerSettings
            {
              ContractResolver = new CamelCasePropertyNamesContractResolver(),
              NullValueHandling = NullValueHandling.Ignore,
              MaxDepth = null,
            };
    

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 2021-11-19
      • 2020-04-27
      • 2020-07-06
      • 2021-12-09
      • 2020-07-26
      • 2021-03-17
      • 2020-05-28
      • 2020-03-30
      相关资源
      最近更新 更多