JsonException: A possible object cycle was detected which is not supported
Today in this article, we will cover below aspects,
Issue Description
ASP.NET Core application gives below runtime error,
System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.
I got the above error for ASP.NET Core 3.1 with EFCore and but this error could possibly exist for 3.* and above any version.
Resolution
I found this error more due to the default JSON serializer used in ASP.NET Core 3.0 and the above version.
ASP.NET Core 3.0 has removed the dependency on JSON.NET and uses it’s own JSON serializer i.e ‘System.Text.Json‘.
ReferenceLoopHandling is currently not supported in the System.Text.Json serializer. This feature will be supported most probably .NET 5 version in the future.
Below was the codebase used which was producing the error for me and i was able to fix the issue using below measures,
I had Repository Pattern implemented with EFCore context,
public async Task<TEntity> Get(string id)
{
return await _context.Set<TEntity>().FindAsync(id.ToString());
}
I was able to fix the issue adding the reference to NewtonsoftJson Nuget package,
PM> Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.1.2
And update the Startup.cs as below,
Similarly, if you have IRouter based routing enabled for API (While migrating .NET Core 2.2 to 3.* version) using EnableEndpointRouting set as false you will get the same error.
Please try using below logic to fix the issue,
References:
Did I miss anything else in these resolution steps?
Did the above steps resolve your issue? Please sound off your comments below!
Happy Coding !!
Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.
Thanks!!! In the case of the .NET CLI use:
dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson –version 5.0.6
https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson/
Thank you Frander !
Grace! My sincere thanks to you Sir,It is helped me to get proper response in .net 5.0 web api.
This is really great. I was stuck for a whole trying to figure out what was wrong with my code and the day after I googled the error I was getting in Postman and like a miracle I came across this thread.
Thanks very much for sharing.
Thanks Siven for the feedback. Glad it helped you!
Thankyou so much. I thought I was doing something wrong and then I came across this. You came like a blessing for my unanswered prayers from heaven above.
Hey Arsalan – Thanks much. Glad it helped you fix the issue!
Thank you so much, it really helped.
Hi Murthy- Glad it helped you!
Well many hours of troubleshooting with no joy, and then I happen upon this… Thank you so much, I can finally move on with my project!
Thank you Bryan- Glad it helped you!
yes it worked. Thank you.
Thanks Kunbi- Glad it helped you.
Good! it worked
Thanks . Glad it helped you.
Thanks bro! It worked 😀
Thanks Frank. Glad it helped you! have a good day.
Thanks so much man! It worked great for me!
Hi! My API is returning data like statemachine, context, moveNext Action, and stay in loop on Insomnia. The node moveNext is very big. How to solve this problem? Thanks.
Hello Strand, If I understood your query correctly, it seems you have an issue with a max depth that is currently limited to 64 by default while using Syste.text.Json. In such case, you may have to overcome the issue using CustomConverter if you want to use Text.JSON else you can use JSON.NET to overcome the issue as there is no default max depth.
Hope this helps. Feel free to revert for any other question.
Many thanks! Solved my problem on tight deadline.
Hello Gary, Thanks. Glad to help you!
You are my hero today. Thank you.
And you are the visitor of the day Alex! Thanks much appreciate your feedback!
Thanks. I had endpoint routing disabled . Using approach 2 worked for me.
Thanks Zito for confirming!
I was able to to resolve the issue using the fix. Thank you!
Great! thank you for confirming