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?
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.
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();
}
For me this helped:
services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With