I'm having a problem with a library installed by a NuGet package not working from PowerShell. I've installed it like this (also tried CurrentUser
scope and a later version of the package but it makes no difference in the end):
Install-Package Microsoft.Bcl.AsyncInterfaces -Version 1.0.0.0
Microsoft.Bcl.AsyncInterfaces
is required by an AWS.Tools
module I'm leveraging, but no matter what I do the module assemblies cannot find it. At first I couldn't even add the type into my own session:
Add-Type -AssemblyName Microsoft.Bcl.AsyncInterfaces
Add-Type: Cannot find path '/home/username/src/work-aws/generate-hostname/Microsoft.Bcl.AsyncInterfaces.dll' because it does not exist.
But I was able to load it directly from the package source directory:
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | Split-Path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
However... the assemblies in the AWS module still can't see it. The errors I'm getting from the AWS
classes are as follows:
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
What am I missing here? I'm not sure how to get this AWS module to see the assembly even after invoking Add-Type
to forcibly load the assembly from its fully-qualified path.
I've posted the output of the Exception
variable here, though everything still indicates Microsoft.Bcl.AsyncInterfaces
can't be found:
ErrorRecord : Exception calling "LoadTable" with "2" argument(s): "The type initializer for 'Amazon.DynamoDBv2.DynamoDBEntryConversion' threw an exception."
WasThrownFromThrowStatement : False
TargetSite : Void ConvertToMethodInvocationException(System.Exception, System.Type, System.String, Int32, System.Reflection.MemberInfo)
StackTrace : at System.Management.Automation.ExceptionHandlingOps.ConvertToMethodInvocationException(Exception exception, Type typeToThrow, String
methodName, Int32 numArgs, MemberInfo memberInfo)
at CallSite.Target(Closure , CallSite , Type , Object , String )
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at System.Management.Automation.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Message : Exception calling "LoadTable" with "2" argument(s): "The type initializer for 'Amazon.DynamoDBv2.DynamoDBEntryConversion' threw an exception."
Data : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException : System.TypeInitializationException: The type initializer for 'Amazon.DynamoDBv2.DynamoDBEntryConversion' threw an exception.
---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The
system cannot find the file specified.
at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
at System.Reflection.RuntimeAssembly.get_DefinedTypes()
at Amazon.DynamoDBv2.DynamoDBEntryConversion.AddConverters(String suffix)
at Amazon.DynamoDBv2.DynamoDBEntryConversion.SetV1Converters()
at Amazon.DynamoDBv2.DynamoDBEntryConversion..ctor(ConversionSchema schema, Boolean isImmutable)
at Amazon.DynamoDBv2.DynamoDBEntryConversion..cctor()
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'Microsoft.Bcl.AsyncInterfaces, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
--- End of inner exception stack trace ---
at CallSite.Target(Closure , CallSite , Type , Object , String )
HelpLink :
Source : System.Management.Automation
HResult : -2146233087
I found that the reason assemblies installed with Install-Package
need to be loaded in this way:
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | split-path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
is because the package/lib directories in the PackagementManagement
provider folders are not part of .NET default probe paths. This is less of a problem when you have modules or projects where the assembly paths are configurable in a file, but in a PowerShell session AFAIK you can't modify the assembly probe path. So you have to load the assembly with the full path.
As for why the AWS assemblies in question couldn't find the assembly name after I loaded it into my session, I found the solution as well. After some additional testing, I found that there is an order of operations here; if you add an assembly with Add-Type
or [Reflection.Assembly]::LoadFile
after another assembly attempts to use it, that second assembly won't be able to find the first assembly for the duration of the PowerShell session. In my case, any assembly making use of Amazon.DynamoDBv2.DynamoDBEntryConversion
in some way was failing because it could never locate Microsoft.Bcl.AsyncInterfaces
.
My issue here was I would load up a new session, and start running my code forgetting to first use Add-Type
to add the Microsoft.Bcl.AsyncInterfaces
assembly, then use the AWS library which relied on it. Once the method failed, I added the assembly but it was too late for that session. So the fix here was to do the following:
# Load the Microsoft.Bcl.AsyncInterfaces assembly
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | split-path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
# Read a DynamoDB table from AWS
$regionEndpoint = [RegionEndPoint]::GetBySystemName('us-west-2')
$client = [AmazonDynamoDBClient]::new($regionEndpoint)
[DocumentModel.Table]::LoadTable($client, 'MyDDBTableName') # This is the call that was failing before, succeeds now