30

My csproject file is indicating: <TargetFramework>netcoreapp3.0</TargetFramework>

In my startup im using the followinhg:

 services.AddMvc(x => x.Filters.AddService<TransactionFilter>())
        .AddJsonOptions(options => options.JsonSerializerOptions... )

But, ReferenceLoopHandling is not available inside options.JsonSerializerOptions.

enter image description here

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FluentNHibernate" Version="2.1.2" />
    <PackageReference Include="FullContact.Contacts.API" Version="1.0.3" />
    <PackageReference Include="Google.Cloud.Storage.V1" Version="2.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Cors" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
    <PackageReference Include="Microsoft.IdentityModel.Tokens" Version="5.5.0" />
    <PackageReference Include="MySql.Data" Version="8.0.17" />
    <PackageReference Include="piplclient" Version="5.0.9" />
    <PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="5.5.0" />
  </ItemGroup>

</Project>
SexyMF
  • 10,657
  • 33
  • 102
  • 206

5 Answers5

59

As part of the work to improve the ASP.NET Core shared framework, Json.NET has been removed from the ASP.NET Core shared framework. Your app may require this reference if it uses Newtonsoft.Json-specific feature such as JsonPatch or converters or if it formats Newtonsoft.Json-specific types.

To use Json.NET in an ASP.NET Core 3.0 project:

Add a package reference to Microsoft.AspNetCore.Mvc.NewtonsoftJson.

Update Startup.ConfigureServices to call AddNewtonsoftJson.

services.AddMvc()
.AddNewtonsoftJson();

This sets up MVC and configures it to use Json.NET instead of that new API. And that AddNewtonsoftJson method has an overload that allows you to configure the Json.NET options like you were used to with AddJsonOptions in ASP.NET Core 2.x.

services.AddMvc()
.AddNewtonsoftJson(options =>
{
    options.SerializerSettings = new JsonSerializerSettings() { … };
});

Reference:

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

https://stackoverflow.com/a/55666898/10201850

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36
  • 19
    Second part should be: `services.AddMvc().AddNewtonsoftJson(options => options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore);` – Bo Søborg Petersen Sep 24 '19 at 16:30
  • 1
    It is one way - to replace native .netcore 3.x serializer with Newton and use old way. Question is why new serializer doesn't have circular reference ignore option. By default they use JsonSerializerOptions.MaxDepth=0, which indicates max depth 64. And if you have loop in object graph it will crash -(((. Weird. May be idea was not to return objects with loop, and produce output which "cleaned" (result of mapping from business entities). – Vitaliy Markitanov Jan 15 '20 at 22:53
33

As of March 2020, the default JSON serializer does not support reference loop handling.

In order to handle that issue, you'll have to first install the older JSON serializer (used in older versions of .NET Core), Microsoft.AspNetCore.Mvc.NewtonsoftJson in the Nuget package manager.

The usage is pretty simple:

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

Or like this if you are using a simple web API:

services.AddControllers().AddNewtonsoftJson(o => 
{
    o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79
javidasd
  • 1,126
  • 13
  • 18
  • 3
    The question relates to supported features/behavior in the new System.Text.Json. Just "adding the old libraries" is not the answer sought for here. – Jonas Stensved Feb 28 '20 at 08:28
6

As mentioned above you need to install Microsoft.AspNetCore.Mvc.NewtonsoftJson, Microsoft.AspNetCore.SignalR.Protocols.Newtonsoft packages and configure with AddNewtonsoftJsonProtocol in order to still use Newtonsoft instead of System.Text.Json (ReferenceLoopHandling not available yet)

For SignalR it would be

services.AddSignalR().AddNewtonsoftJsonProtocol(p => 
{ 
    p.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; 
});
Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79
2
  1. Add a package Microsoft.AspNetCore.Mvc.NewtonsoftJson version - 3.1.3

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

0

MvcNewtonsoftJsonOptions

services.PostConfigure<MvcNewtonsoftJsonOptions>(o => 
{
    o.SerializerSettings.ContractResolver = new MyCustomContractResolver()
    {
        NamingStrategy = new CamelCaseNamingStrategy()
    };

    o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
Nicke Manarin
  • 3,026
  • 4
  • 37
  • 79
Yasser Bazrforoosh
  • 1,246
  • 10
  • 13