Reputation: 7937
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?
Upvotes: 50
Views: 54816
Reputation: 8837
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
Upvotes: 119
Reputation: 25935
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.
Upvotes: 19
Reputation: 11
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>()
}
});
Upvotes: 1
Reputation: 4252
In my case, the solution was to add services.AddControllers()
as described under https://github.com/RicoSuter/NSwag/issues/1961#issuecomment-515631411.
Upvotes: 0
Reputation: 7
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
Upvotes: 1
Reputation: 1296
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;
});
}
Upvotes: 20