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.
4,103 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,815 questions
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 74,296 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

  2. Sibin Thomas 0 Reputation points
    2024-12-09T10:45:55.8333333+00:00

    Which package replaces Microsoft.AspNetCore.Mvc.Formatters.Json on .Net 8.0 ?

    While I am trying to convert .Net Core 2.1 to .Net 8.0 getting this error - 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'.'

    Please tell me which package replaces Microsoft.AspNetCore.Mvc.Formatters.Json on .Net 8.0.

    0 comments No comments

  3. Bruce (SqlWork.com) 74,296 Reputation points
    2024-12-09T16:41:43.9733333+00:00

    Microsoft.AspNetCore.Mvc.NewtonsoftJson is the replacement, but you stated you already converted. most likely another nuget package references it and requires an upgrade or replacement.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.