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

时间:2019-10-13 10:36:44

标签: c# json.net asp.net-core-mvc asp.net-core-3.0 .net-standard-2.1

我在$folders = Get-ChildItem -Recurse -Path C:\temp\1 -Directory $file = "c:\temp\test.txt" foreach($folder in $folders){ $checkFile = $folder.FullName + "\test.txt" $testForFile=Test-Path -Path $checkFile if(!$testForFile){ Copy-Item $file -Destination $folder.FullName } } Web应用程序中使用netstandard2.1库。在netcoreapp3.0中添加服务时,出现以下错误:

  

'无法从以下位置加载类型'Microsoft.AspNetCore.Mvc.MvcJsonOptions'   程序集'Microsoft.AspNetCore.Mvc.Formatters.Json,Version = 3.0.0.0

我还在类库中使用Startup 2.2.0包中的某些功能。

这是我的图书馆Microsoft.AspNetCore.Mvc

.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

这是我的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

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

Startup

请帮助我为什么会出现此错误,并帮助解决该问题?

6 个答案:

答案 0 :(得分:24)

我不确定这是否可以解决OP的问题,但是当您在.Net Core 3中使用Swashbuckle 4时,也会发生此错误。截至撰写本文时,解决方案是使用Swashbuckle 5的预发行版本。即

from kivy.app import App
from kivy.lang import Builder

kv = '''

<MenuSpinner@Spinner>  

<Menu@BoxLayout>
    orientation: 'vertical'
    MenuSpinner:
        text: 'SPINNER'        
        values: ['1', '2']
    BoxLayout:

Menu:
'''   
sm = Builder.load_string(kv)

class TestApp(App):
    def build(self):
        return sm

if __name__ == '__main__':
    TestApp().run()

然后,您需要在Startup.cs中对其进行升级。通常,这涉及为前缀前缀不使用<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc4" /> 编译的类,例如

OpenApi

成为

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

options.SwaggerDoc("v1", OpenApiInfo也变成OpenApiSecurityScheme

另请参阅https://github.com/domaindrivendev/Swashbuckle.AspNetCore上的文档

答案 1 :(得分:10)

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

答案 2 :(得分:2)

出现错误的原因是因为MvcJsonOptions已在.NET Core 3.0中删除;您可以详细了解here的重大更改。

答案 3 :(得分:0)

问题最可能与上述.net core 3.1的不兼容nuget软件包有关。查看您的软件包,并最终升级到核心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

答案 4 :(得分:0)

就我而言,解决方案是按照https://github.com/RicoSuter/NSwag/issues/1961#issuecomment-515631411中的说明添加services.AddControllers()

答案 5 :(得分:0)

当您将配置ApiKeyScheme所需的“ Swashbuckle.AspNetCore”配置为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>()
    }
});