Could not load file or assembly 'Microsoft.OpenApi'... while using Swashbuckle.AspNetCore.Cli

Viewed 3183

I'm getting the subject error message while using the sample MS PowerShell script from the article Generating API clients using AutoRest when the following command line

dotnet swagger "tofile" --output "../../res/swagger.json" "../Sample.Api/bin/Debug/netcoreapp2.1/Sample.Api.dll" v1

is executed. Solution?

FYI: Swashbuckle.AspNetCore.Cli entry source code is located here: Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.Cli/Program.cs

UPDATE

I should have posted the full text of runtime error message - here it is:

At C:\Tests\Swashbuckle\build.ps1:8 char:1
+ dotnet swagger "tofile" --output "../../res/swagger.json" "../Sample. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError

Unhandled Exception:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.OpenApi, 
     Version=1.1.1.0, Culture=neutral, PublicKeyToken=3f5743946376f042'. 
     The system cannot find the file specified.
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)
2 Answers

I have used the following to get rid of this issue.

Upgraded swagger to 5.0.0-rc3, my csproj looks like this

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0-rc3" />
  </ItemGroup>

Then I installed the swagger cli using following code as you must have installed

dotnet tool install -g swashbuckle.aspnetcore.cli --version 5.0.0-rc3

And finally running the command

swagger "tofile" --output "../../res/swagger.json" "../Sample.Api/bin/Debug/netcoreapp2.1/Sample.Api.dll" v1

make sure you have swagger in your path after running the above command.

The issue solved by referencing v.4.0.1 (or v.5.0.0-beta) of Swashbuckle.AspNetCore and Swashbuckle.AspNetCore.Annotations packages and of Swashbuckle.AspNetCore.Cli tool:

...
<ItemGroup>
  <PackageReference Include="Microsoft.AspNetCore.App" />
   <PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
   <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
</ItemGroup>

 <ItemGroup>
    <DotNetCliToolReference Include="Swashbuckle.AspNetCore.Cli" Version="4.0.1" />
 </ItemGroup>
Related