安装适当的 nuget 包后无法在 PowerShell 中使用程序集
Can't use assembly in PowerShell after I've installed the appropriate nuget package
我遇到了 NuGet 包安装的库无法在 PowerShell 中运行的问题。我已经像这样安装了它(也尝试了 CurrentUser
scope 和更高版本的软件包,但最终没有任何区别):
Install-Package Microsoft.Bcl.AsyncInterfaces -Version 1.0.0.0
Microsoft.Bcl.AsyncInterfaces
是我正在利用的 AWS.Tools
模块所必需的,但无论我做什么,模块程序集都找不到它。起初我什至无法将类型添加到我自己的会话中:
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.
但我能够直接从包源目录加载它:
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | Split-Path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
但是...AWS 模块中的程序集仍然看不到它。我从 AWS
类 得到的错误如下:
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.
我在这里错过了什么?我不确定如何让这个 AWS 模块看到程序集,我很困惑为什么在我安装包后 PowerShell 在我调用 Add-Type
.[=22 时没有按名称解析程序集=]
编辑:我在此处发布了 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程序集在我加载到我的session中后找不到程序集名称,我也找到了解决办法。经过一些额外的测试,我发现这里有一个操作顺序;如果在另一个程序集尝试使用 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
我遇到了 NuGet 包安装的库无法在 PowerShell 中运行的问题。我已经像这样安装了它(也尝试了 CurrentUser
scope 和更高版本的软件包,但最终没有任何区别):
Install-Package Microsoft.Bcl.AsyncInterfaces -Version 1.0.0.0
Microsoft.Bcl.AsyncInterfaces
是我正在利用的 AWS.Tools
模块所必需的,但无论我做什么,模块程序集都找不到它。起初我什至无法将类型添加到我自己的会话中:
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.
但我能够直接从包源目录加载它:
Add-Type -Path "$(( Get-Package Microsoft.Bcl.AsyncInterfaces ).Source | Split-Path -Parent)/lib/netstandard2.1/Microsoft.Bcl.AsyncInterfaces.dll"
但是...AWS 模块中的程序集仍然看不到它。我从 AWS
类 得到的错误如下:
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.
我在这里错过了什么?我不确定如何让这个 AWS 模块看到程序集,我很困惑为什么在我安装包后 PowerShell 在我调用 Add-Type
.[=22 时没有按名称解析程序集=]
编辑:我在此处发布了 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程序集在我加载到我的session中后找不到程序集名称,我也找到了解决办法。经过一些额外的测试,我发现这里有一个操作顺序;如果在另一个程序集尝试使用 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