50

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?

6 Answers 6

119

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

Sign up to request clarification or add additional context in comments.

3 Comments

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
@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/…
Thank you, this helped me during our upgrade process
20

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;
            });
}

1 Comment

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.
19

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.

1 Comment

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?
1

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

Comments

1

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>()
    }
});

Comments

0

In my case, the solution was to add services.AddControllers() as described under https://github.com/RicoSuter/NSwag/issues/1961#issuecomment-515631411.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.