安装相应的 nuget 包后,无法在 PowerShell 中使用程序集



我遇到一个问题,NuGet包安装的库无法在PowerShell中运行。我已经这样安装了它(也尝试了CurrentUser范围和包的更高版本,但最终没有什么不同(:

Install-Package Microsoft.Bcl.AsyncInterfaces -Version 1.0.0.0

Microsoft.Bcl.AsyncInterfaces是我正在使用的AWS.Tools模块所必需的,但无论我做什么,模块程序集都找不到它。起初,我甚至无法将类型添加到自己的会话中:

Add-Type -AssemblyName Microsoft.Bcl.AsyncInterfaces

添加类型:找不到路径'/home/username/src/work-aws/generate hostname/Microsoft.Bcl.AncInterfaces.dll',因为它不存在。

但我可以直接从包源目录加载它:

Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | Split-Path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"

然而。。。AWS模块中的程序集仍然看不到它。我从AWS类中得到的错误如下:

System.IO.FileNotFoundException:无法加载文件或程序集"Microsoft.Bcl.AsyncInterfaces,Version=1.0.0.0,Culture=neutral,PublicKeyToken=cc7b13ffcd2ddd51'。系统找不到指定的文件。

这里缺少什么?我不知道如何让这个AWS模块看到程序集,即使在调用Add-Type从其完全限定的路径强制加载程序集之后也是如此。

我已经在这里发布了Exception变量的输出,尽管一切仍然表明找不到Microsoft.Bcl.AsyncInterfaces

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

我发现使用Install-Package安装的程序集需要以这种方式加载的原因:

Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces  ).Source | split-path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"

是因为PackagementManagement提供程序文件夹中的package/lib目录不是.NET默认探测路径的一部分。当您的模块或项目的程序集路径在文件中是可配置的,但在PowerShell会话AFAIK中,您不能修改程序集探测路径时,这就不那么成问题了。因此,必须使用完整路径加载程序集。


至于为什么在我将有问题的AWS程序集加载到会话后找不到程序集名称,我也找到了解决方案。经过一些额外的测试,我发现这里有一个操作顺序;如果在其他程序集尝试使用Add-Type[Reflection.Assembly]::LoadFile的程序集之后添加该程序集,则第二个程序集在PowerShell会话期间将找不到第一个程序集。在我的案例中,任何以某种方式使用Amazon.DynamoDBv2.DynamoDBEntryConversion的程序集都是失败的,因为它永远找不到Microsoft.Bcl.AsyncInterfaces

我的问题是,我会加载一个新的会话,并开始运行我的代码,忘记了首先使用Add-Type添加Microsoft.Bcl.AsyncInterfaces程序集,然后使用依赖它的AWS库。一旦方法失败,我添加了程序集,但对该会话来说为时已晚。因此,这里的解决方案是执行以下操作:

# 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

最新更新