Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json while updgrading .net core 2.2 to 8.0 or 3.1

Manoj Pruthi 0 Reputation points
2024-03-05T00:58:47.3933333+00:00

Hi All While upgrading .Net core 2.2 project to .Net 8.0. I am getting below error. I am using upgrade assistant from Visual studio 2022.

(Same issue if I am upgrading to 3.2 version)

Application startup exception: System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.

at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)

at System.Reflection.RuntimeConstructorInfo.<get_Signature>g__LazyCreateSignature|21_0()

at System.Reflection.RuntimeConstructorInfo.GetParametersNoCopy()

at System.Reflection.RuntimeConstructorInfo.GetParameters()

at Microsoft.Extensions.Internal.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)

at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.ReflectionMiddlewareBinder.CreateMiddleware(RequestDelegate next)

at Microsoft.AspNetCore.Builder.ApplicationBuilder.Build()

at Microsoft.AspNetCore.Hosting.WebHost.BuildApplication()

I have upgraded to nuget package Microsoft.AspNetCore.Mvc.NewtonsoftJson - 8.0.2

Also tried adding below code in startup.cs  services.AddMvc(options => options.EnableEndpointRouting = false)

 .AddNewtonsoftJson();

and

       services.AddControllers()

  .AddNewtonsoftJson();  I am not sure why it is still looking for 2.2 version old nuget package and assembly Microsoft.AspNetCore.Mvc.MvcJsonOptions.

When I tried searching old Microsoft.AspNetCore.Mvc.MvcJsonOptions assembly in project folder, I found in test project in bin\Debug\net8.0\refs folder. Tried to delete it but it is adding it again on build.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,353 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,148 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,196 Reputation points
    2024-03-05T22:09:20.45+00:00

    the Microsoft.AspNetCore.Mvc.Formatters.Json is deprecated, and should not be used with .net 8 applications. remove the package from the project file. You may have another obsolete package that uses it.

    0 comments No comments