处理ASP.NET 5中的JSON循环引用异常

Dmi*_*try 4 c# iis json.net asp.net-core

所以我在ASP.NET 5中使用Web API.在某些时候我的应用程序停止工作,只显示"Bad Gateway"IIS错误页面(我在IIS Express中运行它,由F5).我花了一段时间来弄清问题是什么 - 我在我的Web API方法返回的类中引入了一个循环引用,如下所示:

public class CircularParent
{
    public CircularChild Data;

    public CircularParent()
    {
        Data = new CircularChild(this);
    }
}

public class CircularChild
{
    public CircularParent Owner { get; set; }

    public CircularChild(CircularParent owner)
    {
        Owner = owner;
    }
}
Run Code Online (Sandbox Code Playgroud)

结果是JsonSerializationException.我的问题不是如何解决它,而是如何在将来处理这种情况.我怎么处理这样的例外?或者至少如何记录它或只是看到它记录在某个地方?UseDeveloperExceptionPage()没有帮助.UseExceptionHandler(errorApp => errorApp.Run(...))也没有帮助,执行没有进入errorApp.Run().调试器不会在异常处中断.我得到的所有IIS都是相当无法提供信息的"Bad Gateway"页面.

Ole*_*leg 14

尝试将Newtonsoft.Json最新版本8.0.1-beta3添加到依赖项中package.json并使用

services.AddMvc()
    .AddJsonOptions(options => {
        options.SerializerSettings.ReferenceLoopHandling =
            Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });
Run Code Online (Sandbox Code Playgroud)

有关更多详细信息,请参阅问题