Newtonsoft.Json.JsonSerializationException: Self referencing loop detected with type 'DMDPace.DataAccess.DomainModels.User'. Path 'Department.Users'.

1 Answer 426 Views
Form
kva
Top achievements
Rank 2
Iron
Iron
Iron
kva asked on 14 Aug 2023, 07:02 AM | edited on 17 Aug 2023, 08:53 AM

The form throws this exception when a User has Department. View with form:

@model DMDPace.DataAccess.DomainModels.User;

    @(Html.Kendo().Form<User>()
        .Name("User")
        .HtmlAttributes(new { action = "/Directory/User_Update", method = "POST" })
        .Layout("grid")
        .Grid(g => g.Cols(12).Gutter(20))
        .Items(i =>
        {
            i.Add().Field(e => e.DepartmentId).Label("Отдел").Editor(e => e.DropDownList()
                .DataTextField("Name")
                .DataValueField("Id")
                .DataSource(s =>
                {
                    s.Read(r => r.Action("GetDepartments", "Home"));
                }));
        }))


When I comment form code, it runs without errors.

 

User:

    public class User : Entity
    {
        public int? DepartmentId { get; set; }
        public Department Department { get; set; }
    }

Department:

public class Department : Entity
    {
        public virtual List<User> Users { get; set; }
    }

Startup:

services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

Endpoint which returns view:

[HttpGet("[controller]/User/{Id:int}")] public async Task<IActionResult> UserView(int id) { var entity = await _userObjectService.GetAll() .Include(e => e.Department) .Where(e => e.Id == id) .FirstOrDefaultAsync(); return View("User", entity); }

 

Telerik version - 2022.1.301.

1 Answer, 1 is accepted

Sort by
0
Stoyan
Telerik team
answered on 16 Aug 2023, 01:15 PM

Hello kva,

Thank you for applying code snippets.

However it is difficult for me to understand the full context of the behavior experienced on your side.

Could you please share additional details in regard to the experienced behavior like:

  • the circumstances in which the error occurs;
  • the full error code;
  • the full configuration of the shared files;

These or ideally an isolated sample project that showcases the behavior will enable me to investigate the issue in depth and suggest a possible solution.

Thank you for your cooperation in advance.

Regards,
Stoyan
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.
Stoyan
Telerik team
commented on 22 Aug 2023, 08:56 AM

Hello Kva,

I noticed you have updated your initial message with additional relevant information and snippets.

This allowed me to continue investigating the issue.
 
So far I wasn't able to reproduce the behavior on my side by using the shared snipepts. This leads me to suspect the issue is arises outside of Telerik UI for ASP.NET Core.

Here is what I recommend to resolve the behavior, if possible try replacing the NewtonSoft.Json Serializer with the AddJsonOptions built into the Microsoft.Extensions.DependencyInjection.

Alternatively, explore the suggestions in the related StackOverflow thread JSON.Net Self referencing loop detected.

If you find that I am missing something in the reproduction, please share more information on the topic or ideally, consider sharing an isolated sample project that showcases the reported behavior.

Regards,
Stoyan.

kva
Top achievements
Rank 2
Iron
Iron
Iron
commented on 25 Aug 2023, 06:23 AM | edited

I'm using .net core 3.1 and telerik 2022. Microsoft json serializer doesn't provide ReferenceLoopHandling in this version. And as I said this is the problem with the form, if I delete form from page nothing will throw an exception.
Mihaela
Telerik team
commented on 29 Aug 2023, 08:33 AM

Would you please try to modify the attached runnable sample (.NET Core 3.1, Telerik UI for ASP.NET Core version 2022.1.301) based on your setup in order to reproduce the issue at your end?

Thank you for your cooperation.

Tags
Form
Asked by
kva
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Stoyan
Telerik team
Share this question
or