.NET Core 3 preview 4: 'AddNewtonsoftJson' is not defined

50,797

Solution 1

In order to switch ASP.NET Core 3.0 back to use JSON.NET, you will need to reference the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package. That will contain the AddNewtonsoftJson extension method.

In C#, this would look like this:

services.AddControllers()
    .AddNewtonsoftJson();

So assuming that I understand enough of F#, I would say that your call would be correct if you have the package referenced in your project.

Solution 2

Add package: Microsoft.AspNetCore.Mvc.NewtonsoftJson
Package details: https://www.nuget.org/packages/Microsoft.AspNetCore.Mvc.NewtonsoftJson
Call AddNewtonsoftJson() extension method as mentioned below

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews().AddNewtonsoftJson();
    }

Solution 3

For me this helped:

  1. Code in Startup.cs

services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);

  1. Upgrade all Nuget Packages to 3.1.8 (3.1.3 was not working)

Solution 4

It's work for me, Install the NewtonsoftJson package from NuGet "dotnet add package Microsoft.AspNetCore.Mvc.NewtonsoftJson --version 3.1.0" version 3.1.0 working for ASP.NET Core 3.0 and use the Following Code-

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
        .AddNewtonsoftJson(opt => {
            opt.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        });

Hope it's Working Fine, Thanks.

Share:
50,797
Razor
Author by

Razor

Worked in banking, gaming and ecommerce on sites ranging from 100s to tens of millions of views per day. Front End development is my specialty, and I love all things DevOps - I know a fair bit about these 2, from how to create 3D games on a mobile all the way down to CI pipelines deploying to a kubernetes cluster. I know CSS/JS/HTML inside out, including node. Not a stranger to React, Jest, SCSS, canvas and a great deal of other Front End bits that are not cool anymore. Can find my way around C#, Java, Scala and python. While I don't do much C++ these days, I can still put together a respectable explanation of how a linked lists or hashmap works. Used a fair bit of kubernetes and AWS/azure/google cloud. Love TDD, automated testing and automation in general. I run several services as private projects, including my email server, website and voice chat server.

Updated on July 09, 2022

Comments

  • Razor
    Razor over 1 year

    Using .NET Core 3 preview 4, the "API" template for a F# ASP.NET MVC project fails to build. This is without any changes to the template whatsoever.

    "API" template for a F# ASP.NET MVC project

    This is the code that fails:

    type Startup private () =
        member this.ConfigureServices(services: IServiceCollection) =
            // Add framework services.
            services.AddControllers().AddNewtonsoftJson() |> ignore
    

    With error

    ...\Startup.fs(23,35): error FS0039: The field, constructor or member 'AddNewtonsoftJson' is not defined. Maybe you want one of the following: AddNewtonsoftJsonProtocol

    It seems that there are changes coming for this - is it just being worked on and unusable right now?

  • poke
    poke almost 4 years
    @MortenOC What do you mean by “does not work”? Does it not compile, or is JSON not working? AddControllers and AddControllersWithViews both return the same MvcBuilder (just configured differently), so calling AddNewtonsoftJson() on that should work just fine.
  • Morten OC
    Morten OC almost 4 years
    turns out I was on a wrong package number.. NewtonsoftJson was not used in controllers with .AddControllersWithViews().AddNewtonsoftJson(). Only .AddControllers().AddNewtonsoftJson() worked
  • Pawel Cioch
    Pawel Cioch almost 4 years
    Using Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.0.0 'IServiceCollection' does not contain a definition for 'AddNewtonsoftJson' and the best extension method overload 'NewtonsoftJsonMvcBuilderExtensions.AddNewtonsoftJson(IMvcBu‌​ilder)' requires a receiver of type 'IMvcBuilder'
  • poke
    poke almost 4 years
    @PawelCioch You will need to call AddControllers() or something similar first in order to get an MvcBuilder instance. Then you can call AddNewtonsoftJson() on that.