'无法从程序集'Microsoft.AspNetCore.Mvc.Formatters.Json 加载类型 'Microsoft.AspNetCore.Mvc.MvcJsonOptions',版本 =3.0.0.0

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

我在 netcoreapp3.0 网络应用程序中使用 netstandard2.1 库。在 Startup 中添加我的服务时,出现以下错误:

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

我也在我的 class 库中使用 Microsoft.AspNetCore.Mvc 2.2.0 包中的一些功能。

这是我的图书馆 .csproj,

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
  </ItemGroup>

</Project>

这是我图书馆的 ServiceExtensions class,

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

这是我的 ConfigureLibraryOptions class,

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

这是来自 Startup

ConfigureServices
services.AddControllersWithViews().AddMyLibrary();

请帮助我为什么会收到此错误并协助解决此问题?

您收到错误的原因是因为 MvcJsonOptions 在 .NET Core 3.0 中被删除;您可以阅读有关重大更改的更多信息 here.

我不确定这是否解决了OP的问题,但在.Net Core 3中使用Swashbuckle 4时也会出现此错误。解决方案是使用Swashbuckle 5。 (使用命令 install-package Swashbuckle.AspNetCore)在 .csproj

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

然后您需要在 Startup.cs 中升级它。通常,这涉及不使用 OpenApi 编译的前缀 类,例如

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

变成

options.SwaggerDoc("v1", OpenApiInfo

OpenApiSecurityScheme变成了ApiKeyScheme

另请参阅 https://github.com/domaindrivendev/Swashbuckle.AspNetCore

上的文档

netstandard2.1 到 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;
            });
}

问题很可能是与上面的 .net core 3.1 不兼容的 nuget 包有关。查看您的软件包并最终升级到 core 3.1 的兼容版本。这应该真正解决我与 Automapper 和其他人与 Swagger 之间的问题。

如果您使用的是 AutoMapper,您应该升级到 7.0.0

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

就我而言,解决方案是添加 services.AddControllers(),如 https://github.com/RicoSuter/NSwag/issues/1961#issuecomment-515631411 所述。

当您配置“Swashbuckle.AspNetCore”时,配置 ApiKeyScheme 所需的内容变为 OpenApiSecurityScheme,它正在将方案从

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

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