5

I'm trying to load a .dll of itext7, but if I use this

Add-Type -Path "D:\Eigene\Packages\itext7.7.1.5\lib\net40\itext.kernel.dll"

I get the following exception (translated from german):

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

When I use:

try   { Add-Type -Path "D:\Eigene\Packages\itext7.7.1.5\lib\net40\itext.kernel.dll" }
catch { $_.Exception.LoaderExceptions }

It says (also translated from german):

The File or Assembly "BouncyCastle.Crypto, Version=1.8.1.0, Culture=neutral, PublicKeyToken=0e99375e54769942" or a dependency of it was not found. The system can not find the specified file.

How can I fix this?

EDIT: I found a BouncyCastle dll on my system that it also downloaded when I installed the itext7 package but it also doesn't work, if I load "D:\Eigene\Packages\Portable.BouncyCastle.1.8.5\lib\net40\BouncyCastle.Crypto.dll" before i load the itext.kernel.dll.

Balthazar
  • 463
  • 2
  • 5
  • 13
  • 1
    put both DLL files in the same folder and try again. – TheMadTechnician Apr 04 '19 at 21:28
  • @TheMadTechnician that didn't solve it unfortunately. Could it be that it's a version issue? the bouncy castle dll is 1.8.5 but the error says it wants to load 1.8.1? I'm very new to working with dll – Balthazar Apr 04 '19 at 21:32
  • I didn't even notice the version mismatch. Depends on what the first DLL specifies as a dependent if it wants a specific version or just a minimum version. I'd see if you can get your hands on the exact version of bouncy castle – TheMadTechnician Apr 04 '19 at 21:42
  • @TheMadTechnician great, that worked! – Balthazar Apr 04 '19 at 21:54
  • Very frequently, the Nuget package version doesn't match the version of the DLL inside the package. You should use [ILSpy](https://github.com/icsharpcode/ILSpy/releases) to confirm that the dependency DLLs have the correct versions. – Jeetesh Mangwani May 08 '20 at 08:58

1 Answers1

0

Your assembly is looking for a different version than you have. I'm not sure if you can do an assembly binding in powershell like you could with an application where you could bind an older assembly to a new one example: https://learn.microsoft.com/en-us/dotnet/framework/deployment/configuring-assembly-binding-redirection

Your error is asking you to load the dll of version 1.8.1.0 with the public token of: PublicKeyToken=0e99375e54769942

Generally the assembly publisher isn't changing their public token although it is technically possible bouncy castle could so you likely don't need to specify that.

Anyway you need to find the older version (seems like you have 1.8.5). It is likely these are compatible, but best / safest bet is to use the same assembly the one you are loading needs.

You may find some help here if you need to do binding redirection to the newer assembly: Powershell - Assembly binding redirect NOT found in application configuration file

gdefilippi
  • 35
  • 6
  • This was really old so just posting as I ran into this (similar different assemblies) and fixed it by loading the correct assembly. Haven't messed with binding redirection in Powershell, but use it a lot in .NET – gdefilippi Oct 09 '21 at 10:42