AspNetCore 无法加载类型“Swashbuckle.AspNetCore”

我有 ASP.NET Core Web 应用程序,我在其中使用 swagger 使用以下内容:


public void ConfigureServices(IServiceCollection services)

{

    services.AddMvc();

    services.AddSwaggerGen(c =>

    {

        c.OperationFilter<ExamplesOperationFilter>();

        c.OperationFilter<DescriptionOperationFilter>();

        c.SwaggerDoc("v1", new Info

        {

            Version = "v1",

            Title = "API",

            Description = "",

        });

        // Set the comments path for the Swagger JSON and UI.

        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";

        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);

        c.IncludeXmlComments(xmlPath);

    });

}



public void Configure(IApplicationBuilder app, IHostingEnvironment env)

{

    app.UseSwagger();

    app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API"); });

    if (env.IsDevelopment())

    {

        app.UseDeveloperExceptionPage();

    }


    app.UseMvc();

}

当我导航到 url/swagger 时,出现以下错误:


失败:Microsoft.AspNetCore.Server.Kestrel[13] 连接 ID“0HLG1T7PRT05H”,请求 ID:应用程序抛出了一个未处理的异常。System.TypeLoadException: 无法从程序集“Swashbuckle.AspNetCore.SwaggerGen, Version=3.0.0.0, Culture=neutral”中加载类型“Swashbuckle.AspNetCore.SwaggerGen.SwaggerResponseAttribute”。在 Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.SetResponseModelDescriptions( , ISchemaRegistry schemaRegistry, ApiDescription apiDescription) at Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.Apply(Operation operation, OperationFilterContext context) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistryCorenetschemaRegistry.Asp)1

  apiDescriptions, ISchemaRegistry schemaRegistry)    at

  System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 个源,Func 2 keySelector, Func2 elementSelector,IEqualityComparer 1 comparer)    at


我安装的nuget包是:


Swashbuckle.AspNetCore v3.0.0


Swashbuckle.AspNetCore.Examples v2.9.0


慕尼黑8549860
浏览 213回答 3
3回答

繁星淼淼

卸载软件包Swashbuckle.AspNetCore.Examples应该解决这个问题。新包是(还没试过)-Swashbuckle.AspNetCore.Filters(更新)&nbsp;新包运行良好

烙印99

这对我们有用,同时升级到 .netcore 3.0:1) 安装包 Swashbuckle.AspNetCore -Version 5.0.0-rc42)将代码更改为&nbsp; &nbsp; public void ConfigureServices(IServiceCollection services)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; services.AddSwaggerGen(c =>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPI", Version = "v1" });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }&nbsp; &nbsp; public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ILoggerFactory loggerFactory)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; &nbsp; &nbsp; app.UseSwagger();&nbsp; &nbsp; &nbsp; &nbsp; app.UseSwaggerUI(c =>&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.RoutePrefix = "swagger/ui";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI(v1)");&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; ...&nbsp; &nbsp; }基本上,以下示例位于https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v5.0.0-rc4

波斯汪

将 Swashbuckle.AspNetCore 回滚到 v2.5.0 做到了
打开App,查看更多内容
随时随地看视频慕课网APP