13

I have ASP.NET Core Web application where I am using swagger using the following:

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

When I navigate to url/swagger I am getting the following error:

fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HLG1T7PRT05H", Request id: An unhandled exception was thrown by the application. System.TypeLoadException: Could not load type 'Swashbuckle.AspNetCore.SwaggerGen.SwaggerResponseAttribute' from assembly 'Swashbuckle.AspNetCore.SwaggerGen, Version=3.0.0.0, Culture=neutral'.at Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.SetResponseModelDescriptions(Operation operation, ISchemaRegistry schemaRegistry, ApiDescription apiDescription) at Swashbuckle.AspNetCore.Examples.DescriptionOperationFilter.Apply(Operation operation, OperationFilterContext context) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, ISchemaRegistry schemaRegistry) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, ISchemaRegistry schemaRegistry) at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.CreatePathItems(IEnumerable1 apiDescriptions, ISchemaRegistry schemaRegistry) at Swashbuckle.AspNetCore.SwaggerGen.SwaggerGenerator.GetSwagger(String documentName, String host, String basePath, String[] schemes) at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

The nuget packages I installed are:

Swashbuckle.AspNetCore v3.0.0

Swashbuckle.AspNetCore.Examples v2.9.0

1

5 Answers 5

16

Uninstalling the package

Swashbuckle.AspNetCore.Examples

should fix the issue. The new package is (haven't tried this yet)-

Swashbuckle.AspNetCore.Filters

(UPDATE) The new package works perfectly fine

6

this worked for us, while upgrading to .netcore 3.0:

1) Install-Package Swashbuckle.AspNetCore -Version 5.0.0-rc4

2) change code to

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

basically, following samples found at https://github.com/domaindrivendev/Swashbuckle.AspNetCore/releases/tag/v5.0.0-rc4

1
  • Worked for me in .netcore 3.0
    – iamtravisw
    Commented Nov 12, 2019 at 1:14
4

Rolling back Swashbuckle.AspNetCore to v2.5.0 did the trick

2

This related to several breaking changes in version 3. Here you can find more information.

To fix this try to add Swashbuckle.AspNetCore.Annotations package.

Install-Package Swashbuckle.AspNetCore.Annotations -Version 3.0.0

For dotnet core:

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

Detailed Release Notes For v3.0.0

0
2

To make it work on later versions and in ASPNET Core you need to:

  1. reference nuget Swashbuckle.AspNetCore.Filters
  2. within AddSwaggerGen call ExampleFilters(); (in ConfigureServices)
  3. implement your example class based on IExampleProvider<TargetDtoType> where TargetDtoType is your target type of your example
  4. call AddSwaggerExamplesFromAssemblyOf<ExampleTypeImplementation> where ExampleTypeImplementation is the type from step 3 that implements IExampleProvider<TargetDtoType>

This did the trick for me. No need for any action method attributes, swashbuckle picks up via the parameter type and automatically generates the examples

1
  • Also need to make sure that the Swashbuckle.AspNetCore.Examples package is not included.
    – Alek Davis
    Commented May 6, 2022 at 19:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.