【问题标题】:Can't use assembly in PowerShell after I've installed the appropriate nuget package安装适当的 nuget 包后无法在 PowerShell 中使用程序集
【发布时间】:2020-10-16 21:08:01
【问题描述】:

我遇到了一个由 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.AsyncInterfaces.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 时,PowerShell 没有按名称解析程序集。

编辑:我已经在这里发布了异常变量的输出,因为我省略了几个独特的错误位,尽管所有内容仍然显示 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

【问题讨论】:

    标签: amazon-web-services powershell nuget powershell-core


    【解决方案1】:

    我发现用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
    

    【讨论】:

    • 很高兴听到你想通了。请注意,从 NuGet 包安装所需程序集的Install-Package 方法在这种情况下有效,但如果包具有依赖项则不起作用:请参阅this answer
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2019-04-15
    相关资源
    最近更新 更多