SexyMF
SexyMF

Reputation: 11155

.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>

Upvotes: 30

Views: 42349

Answers (5)

Irina Danovich
Irina Danovich

Reputation: 83

  1. Add a package Microsoft.AspNetCore.Mvc.NewtonsoftJson version - 3.1.3

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

Upvotes: 2

Kamil.Dabrowski
Kamil.Dabrowski

Reputation: 61

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; 
});

Upvotes: 6

javidasd
javidasd

Reputation: 1362

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;
});

Upvotes: 33

Yasser Bazrforoosh
Yasser Bazrforoosh

Reputation: 1296

MvcNewtonsoftJsonOptions

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

    o.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

Upvotes: 1

Xueli Chen
Xueli Chen

Reputation: 12695

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

Upvotes: 60

Related Questions