'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0

35,624

Solution 1

The reason why you're getting the error is because MvcJsonOptions was removed in .NET Core 3.0; you can read more about the breaking changes here.

Solution 2

I'm not sure if this solves OP's problem, but this error also occurs when you use Swashbuckle 4 in .Net Core 3. The solution is to use Swashbuckle 5. (use command install-package Swashbuckle.AspNetCore) to have in .csproj

<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />

Then you'll need to upgrade it in Startup.cs. Generally that involves prefixing classes that don't compile with OpenApi e.g.

options.SwaggerDoc("v1" new Info ...

becomes

options.SwaggerDoc("v1", OpenApiInfo

Also OpenApiSecurityScheme becomes ApiKeyScheme

See also docs at https://github.com/domaindrivendev/Swashbuckle.AspNetCore

Solution 3

netstandard2.1 to netcoreapp3.0 MvcJsonOptions -> MvcNewtonsoftJsonOptions

public IServiceProvider ConfigureServices(IServiceCollection services)
{
            //MVC
            services.AddControllersWithViews(options =>
            {
            }).AddNewtonsoftJson();

            services.PostConfigure<MvcNewtonsoftJsonOptions>(options => {
                options.SerializerSettings.ContractResolver = new MyCustomContractResolver()
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                };
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
            });
}

Solution 4

The problem is most likely with the incompatible nuget packages for .net core 3.1 above. Take a look at your packages and eventually upgrade to compatible version of core 3.1. That should really fix the issue I had one with Automapper and others had with Swagger.

If you are using AutoMapper you should upgrade to 7.0.0

See https://medium.com/@nicky2983/how-to-using-automapper-on-asp-net-core-3-0-via-dependencyinjection-a5d25bd33e5b

Solution 5

When you config "Swashbuckle.AspNetCore", needed for configuring ApiKeyScheme become to OpenApiSecurityScheme it is changing the scheme from

 c.AddSecurityDefinition("Bearer", new ApiKeyScheme { In = "header", Description = 
 "Please enter JWT with Bearer into field", Name = "Authorization", Type = "apiKey" 
 });
 c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>> {
 { "Bearer", Enumerable.Empty<string>() }, });

To

    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
    Description =
        "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
    Name = "Authorization",
    In = ParameterLocation.Header,
    Type = SecuritySchemeType.ApiKey,
    Scheme = "Bearer"
});

c.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
    {
        new OpenApiSecurityScheme
        {
            Reference = new OpenApiReference
            {
                Type = ReferenceType.SecurityScheme,
                Id = "Bearer"
            },
            Scheme = "oauth2",
            Name = "Bearer",
            In = ParameterLocation.Header,

        },
        new List<string>()
    }
});
Share:
35,624
fingers10
Author by

fingers10

I'm an trust worthy, lazy and young self-taught technology lover who likes to architecture and work on bleeding edge technologies. Accomplished secure software developer with more than 6 years of experience in development using Microsoft Tech Stack - .NET and popular front end Tech Stack like HTML5, CSS3 and JavaScript. I make it my mission and help corporates, start-ups, non-profits, open-source to convert their business/ideas into Robust, Accessible and Secure Software Products by providing top notch service and help them survive in their business with success. Whether it's Technology Consultation, Software Development, Solution Architecture, Technology Teaching, I take pride in providing best service in record time while leading with kindness. I have Extensive experience on web based applications built in Microsoft technologies. Hands on experience in working on projects as well as products. Software development experience include developing: Website Designing using HTML, CSS, Bootstrap and jQuery Web Applications using ASP.NET Core Single Page Applications using Angular, Blazor Maintaining and Contributing Open Source Projects I love to make things of my own. My interests revolves on Designing UI and code base (mainly). But also I know my way around Programming, Websites and Web Applications Design and Architecture using Microsoft Server Side Technologies along with Technical Learning and Teaching. When I'm not doing anything above you can find me exploring the world and enjoying the time with my family.

Updated on July 22, 2022

Comments

  • fingers10
    fingers10 almost 2 years

    I'm using netstandard2.1 library in my netcoreapp3.0 web application. When adding my service in Startup, I'm getting the below error:

    'Could not load type 'Microsoft.AspNetCore.Mvc.MvcJsonOptions' from assembly 'Microsoft.AspNetCore.Mvc.Formatters.Json, Version=3.0.0.0

    I'm also using some features from Microsoft.AspNetCore.Mvc 2.2.0 package in my class library.

    Here is my library .csproj,

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <TargetFramework>netstandard2.1</TargetFramework>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
      </ItemGroup>
    
    </Project>
    

    Here is my ServiceExtensions class from my library,

    public static class ServiceExtensions
    {
        public static IMvcBuilder AddMyLibrary(this IMvcBuilder builder)
        {
            builder.Services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            builder.AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver = new DefaultContractResolver();
            });
            builder.Services.ConfigureOptions<ConfigureLibraryOptions>();
    
            return builder;
        }
    }
    

    Here is my ConfigureLibraryOptions class,

    public class ConfigureLibraryOptions : IConfigureOptions<MvcOptions>
    {
        public void Configure(MvcOptions options)
        {
            options.ModelBinderProviders.Insert(0, new CustomBinderProvider());
        }
    }
    

    Here is the ConfigureServices from Startup,

    services.AddControllersWithViews().AddMyLibrary();
    

    Please help on why I'm getting this error and assist on how to solve this?

  • fingers10
    fingers10 over 4 years
    This means I cannot use netstandard2.1 class library with reference to Microsoft.AspNetCore.Mvc 2.2.0 in netcoreapp3.0? Instead I should change the netstandard2.1 to netcoreapp3.0 and add a FrameworkReference to Microsoft.AspNetCore.App?
  • SpiritBob
    SpiritBob over 4 years
    The property Version for the object OpenApiInfo states that it is REQUIRED to be defined. If I skip defining it, everything works, other than not having 2 special labels printed next to my application's name in the UI generated by the swagger (One contains the version we've omitted, the other OAS3. What does it mean?). Why is it required, and what do we signify with it? In the docs you linked, it's defined as v1. Should it stay like that, or is that a mere example -- we should rather store our application's version there instead. It can be anything, such as 0.0.1a-ffg
  • Lee Richardson
    Lee Richardson over 4 years
    @Support Monica - SpiritBob: it goes into the info object of the swagger.json file. It's required because it's required by the spec. I've never had a need to change it from "v1". You can read more about it in the official docs here: github.com/OAI/OpenAPI-Specification/blob/master/versions/…
  • Frank Thomas
    Frank Thomas about 4 years
    This answer would be greatly improved IF you also include the fact that we will need to install the NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson or this will not work.
  • Kasun Koswattha
    Kasun Koswattha almost 4 years
    Thank you, this helped me during our upgrade process