Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 756
Razor Avatar asked Apr 21 '19 21:04

Razor


3 Answers

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.

like image 75
poke Avatar answered Oct 14 '22 21:10

poke


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();
    }
like image 5
Rohit Jadhav Avatar answered Oct 14 '22 20:10

Rohit Jadhav


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)
like image 2
Simon Neubauer Avatar answered Oct 14 '22 20:10

Simon Neubauer