【问题标题】:AspNetCore Could not load type 'Swashbuckle.AspNetCore.SwaggerGen.SwaggerResponseAttribute'AspNetCore 无法加载类型“Swashbuckle.AspNetCore.SwaggerGen.SwaggerResponseAttribute”
【发布时间】:2019-01-21 09:25:29
【问题描述】:

我有 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.SwaggerResponseAttribute' 来自 程序集'Swashbuckle.AspNetCore.SwaggerGen,版本=3.0.0.0, 文化=中性'.at Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.SetResponseModelDescriptions(操作 操作,ISchemaRegistry schemaRegistry,ApiDescription apiDescription) 在 Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.Apply(操作 操作,OperationFilterContext 上下文)在 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry) 在 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, ISchemaRegistry schemaRegistry) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 源,Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable1 apiDescriptions,ISchemaRegistry schemaRegistry)在 Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(字符串 documentName、字符串主机、字符串 basePath、字符串 [] 方案)在 Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext) 在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 申请)

我安装的nuget包是:

Swashbuckle.AspNetCore v3.0.0

Swashbuckle.AspNetCore.Examples v2.9.0

【问题讨论】:

标签: c# asp.net-core swagger


【解决方案1】:

这与版本 3 中的几个重大更改有关。Here you can find more information.

要解决此问题,请尝试添加 Swashbuckle.AspNetCore.Annotations package.

Install-Package Swashbuckle.AspNetCore.Annotations -Version 3.0.0

对于 dotnet 核心:

dotnet add package Swashbuckle.AspNetCore.Annotations --version 3.0.0

详细Release Notes For v3.0.0

【讨论】:

    【解决方案2】:

    将 Swashbuckle.AspNetCore 回滚到 v2.5.0 成功了

    【讨论】:

      【解决方案3】:

      卸载包

      Swashbuckle.AspNetCore.Examples
      

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

      Swashbuckle.AspNetCore.Filters
      

      (更新) 新包运行良好

      【讨论】:

        【解决方案4】:

        要使其在更高版本和 ASPNET Core 中运行,您需要:

        1. 参考 nuget Swashbuckle.AspNetCore.Filters
        2. AddSwaggerGen 内调用ExampleFilters();(在ConfigureServices 中)
        3. 基于IExampleProvider&lt;TargetDtoType&gt; 实现示例类,其中TargetDtoType 是示例的目标类型
        4. call AddSwaggerExamplesFromAssemblyOf&lt;ExampleTypeImplementation&gt; 其中 ExampleTypeImplementation 是步骤 3 中实现 IExampleProvider&lt;TargetDtoType&gt; 的类型

        这对我有用。不需要任何action方法属性,swashbuckle通过参数类型拾取并自动生成示例

        【讨论】:

          【解决方案5】:

          这对我们有用,同时升级到 .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找到以下样本

          【讨论】:

          • 在 .netcore 3.0 中为我工作
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-08-14
          • 2015-08-28
          • 2016-03-05
          • 2014-08-09
          • 2015-04-16
          • 2010-09-08
          • 2017-04-13
          相关资源
          最近更新 更多