代码之家  ›  专栏  ›  技术社区  ›  Yahya Hussein

AspNetCore无法加载类型“swashbuck.AspNetCore.SwaggerGen.SwaggerResponseAttribute”

  •  1
  • Yahya Hussein  · 技术社区  · 7 年前

    我有一个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] 申请。System.TypeLoadException:无法加载类型 来自 区域性=中性'.at swashbacker.AspNetCore.Examples.DescriptionOperationFilter.SetResponseModelDescriptions(操作 apiDescription)在 swashbucker.AspNetCore.Examples.DescriptionOperationFilter.Apply(操作 操作,OperationFilterContext context)位于 swashbacker.AspNetCore.swagggergen.swagggergenerator.CreateOperation(ApiDescription swashbacker.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable 1 apiDescriptions, ISchemaRegistry schemaRegistry) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable 来源,Func 2 keySelector, Func IEqualityComparer公司 1 comparer) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable 1个 swashbuck.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(字符串 documentName,String host,String basePath,String[]schemes)位于 httpContext)在 Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHTTP应用程序'1

    我安装的nuget包是:

    swashbucker.AspNetCore.Examples版本2.9.0

    4 回复  |  直到 7 年前
        1
  •  5
  •   IeuanW    7 年前

    卸载包

    Swashbuckle.AspNetCore.Examples
    

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

    Swashbuckle.AspNetCore.Filters
    

    (更新) 新的包装很好用

        2
  •  4
  •   Yahya Hussein    7 年前

        3
  •  4
  •   yob    6 年前

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

    1) 安装软件包Swashbuckle.AspNetCore-版本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

        4
  •  1
  •   Nick Suslov    7 年前

    这与版本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

        5
  •  0
  •   Ricardo Rodrigues    7 年前

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

    1. 参考nuget swashbleck.AspNetCore.Filters
    2. 在内部 AddSwaggerGen 呼叫 ExampleFilters(); (在配置服务中)
    3. IExampleProvider<TargetDtoType> 哪里 TargetDtoType 你的目标类型是你的例子
    4. 呼叫 AddSwaggerExamplesFromAssemblyOf<ExampleTypeImplementation> IExampleProvider<TargetDtoType>