为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
慕尼黑8549860 2021-11-28 16:12:52
我有 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.0Swashbuckle.AspNetCore.Examples v2.9.0
查看完整描述

3 回答

?
繁星淼淼

TA贡献1775条经验 获得超11个赞

卸载软件包

Swashbuckle.AspNetCore.Examples

应该解决这个问题。新包是(还没试过)-

Swashbuckle.AspNetCore.Filters

(更新) 新包运行良好


查看完整回答
反对 回复 2021-11-28
?
烙印99

TA贡献1829条经验 获得超13个赞

这对我们有用,同时升级到 .netcore 3.0:


1) 安装包 Swashbuckle.AspNetCore -Version 5.0.0-rc4


2)将代码更改为


    public void ConfigureServices(IServiceCollection services)

    {

        ...

        services.AddSwaggerGen(c =>

                {

                    c.SwaggerDoc("v1", new OpenApiInfo { Title = "WebAPI", Version = "v1" });

                });

        ...

    }


    public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ILoggerFactory loggerFactory)

    {

        ...

        app.UseSwagger();

        app.UseSwaggerUI(c =>

        {

            c.RoutePrefix = "swagger/ui";

            c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebAPI(v1)");

        });

        ...

    }

基本上,以下示例位于https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v5.0.0-rc4


查看完整回答
反对 回复 2021-11-28
?
波斯汪

TA贡献1811条经验 获得超4个赞

将 Swashbuckle.AspNetCore 回滚到 v2.5.0 做到了


查看完整回答
反对 回复 2021-11-28
  • 3 回答
  • 0 关注
  • 212 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信