Legion Developers

Sharing Knowledge

Solving “A possible object cycle was detected”. in .NET

To solve the problem:

System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles.

There are at least two options:

  1. Remove circular references.
  2. Ignore circular references.

The easy way to fix it is to ignore circular references, to do this we need to add this piece of code to services configuration:

services.AddControllers().AddJsonOptions(x => x.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.Preserve);
Don’t forget to check my previous posts
Solving “A possible object cycle was detected”. in .NET

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top