Self referencing loop detected

sblb 1,231 Reputation points
2023-07-03T19:57:39.4733333+00:00

Hi,

I received this message but I don't know really what does it mean!

Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'suiviBE' with type 'FollowUpDash.Shared.Models.SuiviBE'. Path '[0].actionItems[0]'.

To fix it I add in program.cs

builder.Services.AddControllers().AddNewtonsoftJson(options =>
    {   
    options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });

But I don't know what' happen in my code. Have you an idea why I received this message?

Thanks in advance!

Developer technologies .NET Blazor
{count} votes

Accepted answer
  1. Anonymous
    2023-07-04T01:55:27.49+00:00

    Hi @sblb

    Refer to the following sample:

    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public virtual ICollection<Post> Posts { get; set; }
    }
    
    public class Post
    {
        public int Id { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    
        public virtual Blog Blog { get; set; }
    }
    

    Because EF Core automatically does fix-up of navigation properties, you can end up with cycles in your object graph. For example, loading a blog and its related posts will result in a blog object that references a collection of posts. Each of those posts will have a reference back to the blog.

    Some serialization frameworks don't allow such cycles. For example, Json.NET will throw the following exception if a cycle is found.

    Newtonsoft.Json.JsonSerializationException: Self referencing loop detected for property 'Blog' with type 'MyApplication.Models.Blog'.

    More detail information, see Related data and serialization.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion


1 additional answer

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-07-05T12:56:38.23+00:00

    What do you think about that? Can I stay with that or do you have change the code?

    There are issues with using Entities as a Web API interface.

    The design opens up Web API for over posting. Entities model the table schema. It is very common to have an update scenario where the user updates only a few of the column not all columns in a table. This can cause column values to get overwritten by default values. It also allows malicious actors to add values to the post that you did not expect.

    https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0

    Another issue is unexpected validation errors. This happens when Entities contain navigation properties. Let's assume the intent is to add or update an Entity that has a child relationship. The add or update can cause a validation error in the child record. For example, if there a required field or a non-null property. Now you're faced with balancing model validation with table schema design.

    The solution is to use View Models and DTOs. Please see your prior threads with these issues and the community's recommendations.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.