Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self referencing loop detected in ASP.NET Core [duplicate]

When I try to serialize some domain objects using ASP.NET Core Newsoft JSON.NET it is throwing an exception because it is detecting a self referencing loop.

In ASP.NET 4 we used to fix it globally this way: JSON.NET Error Self referencing loop detected for type

How can we fix this in ASP.NET Core?

like image 708
sunil Avatar asked Jan 12 '16 20:01

sunil


1 Answers

There is no difference in the way self-referencing loops are handled in ASP.NET 4 compared to ASP.NET Core (previously Asp.Net 5). The principles outlined in the question you referenced in your post still apply. However, setting this property in ASP.NET Core is obviously slightly different, given the new method of configuring and bootstrapping the app:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc().AddJsonOptions(options => {
        options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
        options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
    });
    services.AddEntityFramework().AddSqlServer().AddDbContext<IvoryPacketDbContext>(
        options => options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"])
    );
}
like image 81
Blake Mumford Avatar answered Nov 13 '22 04:11

Blake Mumford