如何修复 PowerShell 中的 DLL LoaderException

Bal*_*zar 6 dll powershell dllimport

我正在尝试加载.dllitext7 的 a,但如果我使用这个

Add-Type -Path "D:\Eigene\Packages\itext7.7.1.5\lib\net40\itext.kernel.dll"
Run Code Online (Sandbox Code Playgroud)

我收到以下异常(翻译自德语):

Add-Type : Add-Type : Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
In Zeile:2 Zeichen:1
+ Add-Type -Path "D:\Eigene\Packages\itext7.7.1.5\lib\net40\itext.kerne ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Add-Type], ReflectionTypeLoadException
    + FullyQualifiedErrorId : System.Reflection.ReflectionTypeLoadException,Microsoft.PowerShell.Commands.AddTypeCommand
Run Code Online (Sandbox Code Playgroud)

当我使用时:

try   { Add-Type -Path "D:\Eigene\Packages\itext7.7.1.5\lib\net40\itext.kernel.dll" }
catch { $_.Exception.LoaderExceptions }
Run Code Online (Sandbox Code Playgroud)

它说(也是从德语翻译的):

未找到文件或程序集“BouncyCastle.Crypto,Version=1.8.1.0,Culture=neutral,PublicKeyToken=0e99375e54769942”或其依赖项。系统找不到指定的文件。

我怎样才能解决这个问题?

编辑:我在我的系统上发现了一个 BouncyCastle dll,当我安装 itext7 软件包时它也会下载,但如果我"D:\Eigene\Packages\Portable.BouncyCastle.1.8.5\lib\net40\BouncyCastle.Crypto.dll"在加载itext.kernel.dll.

小智 0

您的程序集正在寻找与您不同的版本。我不确定您是否可以在 powershell 中进行程序集绑定,就像使用应用程序一样,您可以将旧程序集绑定到新的示例: https: //learn.microsoft.com/en-us/dotnet/framework /部署/配置程序集绑定重定向

您的错误是要求您使用以下公共令牌加载版本 1.8.1.0 的 dll:PublicKeyToken=0e99375e54769942

一般来说,程序集发布者不会更改其公共令牌,尽管从技术上讲,充气城堡可以,因此您可能不需要指定这一点。

无论如何,你需要找到旧版本(好像你有 1.8.5)。这些很可能是兼容的,但最好/最安全的选择是使用您正在加载所需的相同程序集。

如果您需要对较新的程序集进行绑定重定向,您可以在这里找到一些帮助:Powershell - 在应用程序配置文件中找不到程序集绑定重定向