Could not load type 'Swashbuckle.AspNetCore.Swagger.SwaggerDocument' from assembly

2.5k views Asked by At

At the moment I'm trying to update a project from dotnet 2.1 to 3.1. To do that and avoid exceptions I've had to upgrade swagger, and everything works, except swagger. For some unknown reason Swagger returns this exception when I try to produce it's json mapping. I'm making these changes in a Catalina macOs, macBook Pro.

 The command "dotnet swagger tofile --output .\autogenerated-api.json bin/Debug/netcoreapp3.1/App.dll v2" exited with code 134.
...

 Unhandled exception. System.TypeLoadException: Could not load type 'Swashbuckle.AspNetCore.Swagger.SwaggerDocument' from assembly 'Swashbuckle.AspNetCore.Swagger, Version=5.6.3.0, Culture=neutral, PublicKeyToken=62657d7474907593'.
 at Swashbuckle.AspNetCore.Cli.Program.<>c.<Main>b__0_3(IDictionary`2 namedArgs)
 at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args)
 at Swashbuckle.AspNetCore.Cli.CommandRunner.Run(IEnumerable`1 args)
 at Swashbuckle.AspNetCore.Cli.Program.Main(String[] args)

So far I haven't found any information about that exception specifically. What's going wrong?. I'm using latest Swagger along with a pretty plain setting:

<PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="5.6.3" />

...

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="dotnet swagger tofile --output .\autogenerated-api.json $(OutputPath)$(AssemblyName).dll v2" />

And the setting in Startup.cs:

services.AddSwaggerGen(c =>
    {
            c.SwaggerDoc("v2", new OpenApiInfo { Title = "App Api", Version = "v2"});
    });

...

            if (env.IsDevelopment())
        {
            app.UseSwagger();

            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v2/swagger.json", $"{nameof(App)} API V2");
                c.InjectStylesheet("/brand.css");
            });
        }

The idea naturally is to generate a json mapping on every development run. Still wondering why Swagger is trying to reach a package it doesn't have (SwaggerDocument).

Thanks.

1

There are 1 answers

0
Ahmed Ali On

It could be pointing to different version of CLI.

Try the following steps.

  1. Go to project root
  2. Open a console
  3. Create a manifest file

dotnet new tool-manifest  

  1. List item

Install the 5.6.3 CLI

dotnet tool install --version 5.6.3 Swashbuckle.AspNetCore.Cli

  1. Generate a swagger output file . {replace Project with your ProjectName.dll}

dotnet swagger tofile --output swagger.json bin/Debug/netcoreapp3.1/Project.dll v1