question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

services.AddControllers().AddNewtonsoftJson() doesn't override System.Text.Json

See original GitHub issue

From @Yannick183 on Wednesday, August 28, 2019 8:16:27 AM

Issue Title

services.AddControllers().AddNewtonsoftJson() doesn’t seem to override System.Text.Json

General

I tried to upgrade a WebAPI project from dotnetcore 2.2 to version 3 but I’m facing some issues with Json serialization.

Using version 3.0.100-preview8-013656, I tried to override System.Text.Json by adding this to ConfigureServices

services.AddControllers().AddNewtonsoftJson(options =>
{
    options.SerializerSettings.Converters.Add(new StringEnumConverter(new CamelCaseNamingStrategy()));
    options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

Some of my controller endpoints fail with a HTTP500 error because of some circular references that should have been prevented by the [JsonIgnore] attribute. The error is the following : System.Text.Json.JsonException: CurrentDepth (32) is equal to or larger than the maximum allowed depth of 32.

As you can see, the error comes from System.Text.Json.JsonException which I suspect means that Newtonsoft.Json isn’t used by the controller to serialize the response.

I use ModelMetadataType to add [JsonIgnore] to some attributes like this. I even tried adding System.Text.Json.Serialization.JsonIgnore to the property but without any result.

    [ModelMetadataType(typeof(ISomeEntityClassMetadata))]
    public partial class SomeEntityClass : ISomeEntityClassMetadata
    {
    }

    public interface ISomeEntityClassMetadata
    {
        [System.Text.Json.Serialization.JsonIgnore]
        [JsonIgnore]
        SomeOtherEntityClass SomeOtherEntityNavigation { get; set; }
    }

Copied from original issue: dotnet/core#3269

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
anaximander23commented, Sep 24, 2019

Just ran into this myself; I have to say, it’s very strange that the .AddNewtonsoftJson() method behaves in this way - that is, that it’s available, and doesn’t throw an error, when the relevant NuGet package isn’t even installed.

  • If it’s doing something useful (ie. something other than replacing the deserialiser in use during model binding) then maybe the method name should be made more explicit to clarify what it actually does?
  • If it’s not doing something useful (ie. it’s intended solely to change the deserialiser in use during model binding, and without the NuGet package it just silently does nothing) then perhaps it should be declared inside of that NuGet package so that it’s not accessible when it’s not going to have the desired effect?

Either way, while you’re right, it would be nice if the documentation properly explained how this should work, it would be even nicer if the code didn’t let developers get into this baffling situation.

11reactions
Yannick183commented, Sep 2, 2019

OK now I feel stupid. @remyjette I realized I didn’t have Microsoft.AspNetCore.Mvc.NewtonsoftJson I only had Newtonsoft.Json installed but since I had the .AddNewtonsoftJson() extension method available I thought it was enough. I was unable to find it in NuGet GUI Microsoft.AspNetCore.Mvc.NewtonsoftJson so my guess was that package was used for previous versions only. I then tried to install it from command line Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson -Version 3.0.0-preview8.19405.7 and it worked. Now System.Text.Json is overriden as expected and the circular reference problem is gone.

I don’t know why I couldn’t find the package from NuGet GUI though. You can see below that the search term doesn’t provide any result for Microsoft.AspNetCore.Mvc.NewtonsoftJson but the command line worked.

Using Visual Studio 2019 16.3.0 Preview 2.0

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

AddNewtonsoftJson is not overriding System.Text.Json
AddNewtonsoftJson in ConfigureServices seemingly does nothing, and the new Json serializer seems to work on properties only, not fields. It does ...
Read more >
Migrate from Newtonsoft.Json to System.Text.Json - .NET
Json. The System.Text.Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON).
Read more >
Using Newtonsoft.Json In .NET Core 3+ Projects
We are migrating API from Core 2.2 to Core 3.1 and we do have only services.mvc() method. After that we used AddNewtonsoftJson() but...
Read more >
Using multiple JSON serialization settings in ASP.NET Core
Unfortunately, ASP.NET Core only lets us specify global JSON serialization settings, using .AddControllers().AddJsonOptions(.
Read more >
Try the new System.Text.Json APIs - .NET Blog
Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. In ConfigureServices() add a call to AddNewtonsoftJson() ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found