Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference Loop Handling Ignore not working on Asp.Net Core 3.0 Preview 3

I have been beating my head against a wall with this one, trying to find out why it won't work. I haven't been able to find anything on why it won't work, so I am asking here.

I have a console application that is running on Asp.Net Core 3.0 Preview 3.

On this project I am getting a Json loop problem, which I know I can fix with setting the Reference Loop Handling in Startup to Ignore. However, I could only find information on setting it inside the .AddJsonOptions(), which doesn't appear to be in Asp.Net Core 3.0.

I went to the documentation for how to migrate from 2.1 to 3.0 and I found this

Even after changing my code accordingly

services.AddMvc()
     .AddNewtonsoftJson(
          options => { options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }
      );

I still get an error saying: "Self referencing loop detected for property '[insert class name]' with type '[model name]'."

Where else can I set Json to ignore the loop reference?

Or what can I do to make this work?

Thank you in advanced

like image 849
DalTron Avatar asked Mar 19 '19 17:03

DalTron


2 Answers

As explained in detail here as part of ASP.NET Core 3.0, the team moved away from including Newtonsoft.Json by default.

You probably need to install Microsoft.AspNetCore.Mvc.NewtonsoftJson and use (note, I'm using .AddNewtonsoftJson() chained with .AddControllers()) something similar:

services.AddControllers()
    .AddNewtonsoftJson(x =>
            {
                x.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });
like image 101
Filipe Madureira Avatar answered Sep 20 '22 18:09

Filipe Madureira


services.AddMvc().AddNewtonsoftJson(options=> options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.0&tabs=visual-studio#jsonnet-support

like image 41
Sato Avatar answered Sep 18 '22 18:09

Sato