无法加载文件或程序集"Microsoft.OpenApi"...同时使用 Swashbuckle.AspNetCore.Cli


当以下命令行使用AutoRest生成API客户端时,

使用文章使用AutoRest生成API客户端中的示例MS PowerShell脚本时收到主题错误消息

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

被执行。溶液?

仅供参考:Swashbuckle.AspNetCore.Cli 条目源代码位于此处:Swashbuckle.AspNetCore/src/Swashbuckle.AspNetCore.Cli/Program.cs

更新

我应该发布运行时错误消息的全文 - 这里是:

At C:TestsSwashbucklebuild.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)

我使用了以下内容来解决这个问题。

摇大摆升级到 5.0.0-rc3,我的 csproj 看起来像这样

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

然后我使用以下代码安装了 swagger cli,因为您必须已安装

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

最后运行命令

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

确保在运行上述命令后,您的路径中大摇大摆。

通过引用 Swashbuckle.AspNetCore 和 Swashbuckle.AspNetCore.Annotations 包的 v.4.0.1(或 v.5.0.0-beta)以及 Swashbuckle.AspNetCore.Cli 工具解决了这个问题:

...
<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>

最新更新