【问题标题】:Could not load file or assembly 'Microsoft.OpenApi'... while using Swashbuckle.AspNetCore.Cli使用 Swashbuckle.AspNetCore.Cli 时无法加载文件或程序集“Microsoft.OpenApi”...
【发布时间】:2019-05-26 16:48:24
【问题描述】:

我在使用文章 Generating API clients using AutoRest 中的示例 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:\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)

【问题讨论】:

    标签: c# reflection dependencies asp.net-core-2.0 assembly-loading


    【解决方案1】:

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

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

    【讨论】:

    • 您使用的是哪个版本的 .Net core?它是否适用于 .Net core 3.1?
    【解决方案2】:

    我使用以下方法解决了这个问题。

    swagger升级到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
    

    确保在运行上述命令后,您的路径中包含了 swagger。

    【讨论】:

      猜你喜欢
      • 2017-01-17
      • 2020-08-17
      相关资源
      最近更新 更多