Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core 3 not having ReferenceLoopHandling in AddJsonOptions

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>
like image 648
SexyMF Avatar asked Sep 19 '19 07:09

SexyMF


3 Answers

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://docs.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

like image 56
Xueli Chen Avatar answered Nov 13 '22 16:11

Xueli Chen


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; }); 
like image 43
javidasd Avatar answered Nov 13 '22 15:11

javidasd


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; 
});
like image 37
Kamil.Dabrowski Avatar answered Nov 13 '22 16:11

Kamil.Dabrowski