profile
viewpoint

Ask questionsRestore MvcJsonOptions to ASP.NET Core 3.0 as a type forward?

Problem

As originally brought up in #7220, the removal of Newtonsoft.Json as a core dependency is causing breaking application changes for applications exposing Swagger/Open API documentation using existing popular libraries such as Swashbuckle.AspNetCore (https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/1030) and NSwag (https://github.com/RSuter/NSwag/issues/1961#issuecomment-470288203).

These projects (and possibly others) have been built to rely on the MvcJsonOptions type present in earlier versions of ASP.NET Core and its presence in the DI system to access JsonSerializerSettings for JSON serialization purposes.

These errors manifest as TypeLoadException in ASP.NET Core 3.0 preview 3, and cannot easily be resolved without binary breaking changes to such libraries.

System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Signature..ctor(IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeConstructorInfo.get_Signature()
   at System.Reflection.RuntimeConstructorInfo.GetParametersNoCopy()
   at System.Reflection.RuntimeConstructorInfo.GetParameters()
   at Microsoft.Extensions.Internal.ActivatorUtilities.ConstructorMatcher..ctor(ConstructorInfo constructor)
   at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
   at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass4_0.<UseMiddleware>b__0(RequestDelegate next)
   at Microsoft.AspNetCore.Builder.Internal.ApplicationBuilder.Build()
   at Microsoft.AspNetCore.Hosting.Internal.GenericWebHostService.StartAsync(CancellationToken cancellationToken)

Suggested Solution

To provide an improved migration path for such applications where the presence of Newtonsoft.Json as a dependency is not an issue, could the MvcJsonOptions class be restored as type-forwarded from the Microsoft.AspNetCore.Mvc.Formatters.Json assembly to Microsoft.AspNetCore.Mvc.NewtonsoftJson?

The forwarded type could be marked as [Obsolete] to discourage use and removed in ASP.NET Core 4.0, while providing a "crutch" of sort to prevent blockers and/or delays to adoption of ASP.NET Core 3.0 in existing ASP.NET Core 2.x applications due to the extensive usage of Newtonsoft.Json in non-Microsoft libraries.

aspnet/AspNetCore

Answer questions JohnGalt1717

This is a blocker. It basically makes any API layer unusable in .NET Core 3 Preview 3 because they'll all depend on this working if they have swagger documention which is essentially all of them.

useful!

Related questions

HTTP Error 500.31 - ANCM Failed to Find Native Dependencies hot 8
ERR_HTTP2_INADEQUATE_TRANSPORT_SECURITY when clean webapi template
MissingMethodException: Method not found Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder
Migrate Asp.Net Core 2.2 Integration Tests to 3.0 - The TestServer constructor was not called with a IWebHostBuilder so IWebHost is not available. hot 4
UseStaticFiles with StaticFileOptions/FileExtensionContentTypeProvider breaks server side Blazor hot 3
SignalR .NetCore 3.0-- HubConnectionBuilder' does not contain a definition for 'WithUrl' hot 3
HTTP Error 500.30 - ANCM In-Process Start Failure hot 3
With Asp.net core 3.0 SignInManager.IsSignedIn(User) allways reuturn false?but in 2.2 it works fine.Is that a debug? hot 3
CSS Isolation in Blazor Components hot 3
How to retrieve the HttpContext from Blazor server-side ? hot 3
Oauth2 authentication erroring with "The oauth state was missing or invalid." hot 3
Failed to gracefully shutdown application (IIS Hang) hot 3
BadHttpRequestException: Reading the request body timed out due to data arriving too slowly hot 2
HTTP Error 500.0 - ANCM In-Process Handler Load Failure hot 2
Method not found: 'Microsoft.EntityFrameworkCore.Metadata.Builders.IndexBuilder Microsoft.EntityFrameworkCore.Metadata.Builders.EntityTypeBuilder`1.HasIndex(System.Linq.Expressions.Expression`1<System.Func`2<System.__Canon,System.Object>>)'. hot 2
Github User Rank List