'.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.
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?
Solution 1:[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:[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:[3]
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.
Solution 4:[4]
For me this helped:
- Code in Startup.cs
services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
- Upgrade all Nuget Packages to 3.1.8 (3.1.3 was not working)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Rohit Jadhav |
| Solution 3 | |
| Solution 4 | Simon Neubauer |

