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

Razor picture Razor · Apr 21, 2019 · Viewed 31.4k times · Source

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?

Answer

poke picture poke · Jun 11, 2019

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.