43

When I try to serialize some domain objects using ASP.NET Core Newsoft JSON.NET it is throwing an exception because it is detecting a self referencing loop.

In ASP.NET 4 we used to fix it globally this way: JSON.NET Error Self referencing loop detected for type

How can we fix this in ASP.NET Core?

Community
  • 1
  • 1
sunil
  • 5,078
  • 6
  • 28
  • 33
  • Look at [the answer](http://stackoverflow.com/a/34421722/315935). It should solve your problem. – Oleg Jan 12 '16 at 23:37
  • You might have a look at my answer on **[“Self Referencing Loop Detected” exception with JSON.Net](https://stackoverflow.com/questions/40472419/self-referencing-loop-detected-exception-with-json-net/51235783#51235783)** page. – Murat Yıldız Jul 08 '18 at 20:36
  • The "duplicate" question is only a partial duplicate, since it doesn't refer to the particularities of integrating this in ASP.NET Core. As the accepted answer says, things have changed in Core so the answer of the "duplicate" question wouldn't work. – Vlad Iliescu Aug 09 '18 at 09:06

1 Answers1

109

There is no difference in the way self-referencing loops are handled in ASP.NET 4 compared to ASP.NET Core (previously Asp.Net 5). The principles outlined in the question you referenced in your post still apply. However, setting this property in ASP.NET Core is obviously slightly different, given the new method of configuring and bootstrapping the app:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddJsonOptions(options => {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
    services.AddEntityFramework().AddSqlServer().AddDbContext<IvoryPacketDbContext>(
        options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
    );
}
Nkosi
  • 235,767
  • 35
  • 427
  • 472
Blake Mumford
  • 17,201
  • 12
  • 49
  • 67
  • 22
    to be clear, only the "ReferenceLoopHandling" line is required to resolve this issue. – Daniel Jan 25 '18 at 18:24
  • 1
    Before I found this solution, I tried to use that setting on the property that was causing issues, as an attribute `[JsonProperty(ReferenceLoopHandling=ReferenceLoopHandling.Ignore)]` but it had no effect. Can anyone explain why this solution didn't work in the first place? – Pablo Recalde Feb 09 '18 at 10:33
  • 1
    HI.. it does not work for me neither.. I only had to add "[JsonObject(IsReference = true)] " as a header on the class that gave me error and Works fine. – Diego Feb 21 '18 at 15:29
  • 2
    Decorating the property with `[JsonIgnore]` solved it for me – saviour123 Jul 02 '18 at 09:55
  • 1
    Setting `ReferenceLoopHandling` in `JsonProperty` on the attribute together with `[JsonObject(IsReference = true)]` solved the problem. The original answear with `AddJsonOptions` doesn't had any effect for me. – Lion Dec 12 '18 at 01:07
  • AddJsonOptions is the great solution that fixed incomplete json response issue without any hassle. – Saima Jun 19 '19 at 11:39
  • 2
    you can avoid the self reference possibility by selecting only fields you need and exclude the fields that generate the reference looping – Hamza Dahmoun Apr 20 '20 at 16:16