PowerShell cmdlet在运行时找不到NuGet程序集

时间:2019-02-14 22:31:06

标签: c# powershell powershell-core

我创建了一个C#类库,该库将承载许多PowerShell Core(6.1.2)Cmdlet。

该项目包括两个NuGet软件包:

  • System.Management.Automation(用于Core)(6.1.2)
  • Oracle.ManagedDataAccess.Core(2.18.3)

New-Session Cmdlet:

using System;
using System.Management.Automation; 
using Oracle.ManagedDataAccess.Client; 
using Oracle.ManagedDataAccess.Types;

namespace PsOracleCore.Cmdlets
{
  [Cmdlet(VerbsCommon.New, "Session")]
  [OutputType(typeof(OracleConnection))] 
  public class NewSessionCmdlet : Cmdlet 
  {
    [Parameter(Position = 0)]
    [ValidateNotNullOrEmpty]
    public string Account { get; set; }

    [Parameter(Position = 1)]
    [ValidateNotNullOrEmpty] 
    public string Password { get; set; }

    [Parameter(Position = 2)]
    [ValidateNotNullOrEmpty] 
    public string DataSource { get; set; }

    protected override void ProcessRecord()
    {
      string connectionString = String.Format("User Id={0};Password={1};Data Source={2};",this.Account,this.Password,this.DataSource);

      OracleConnection connection = new OracleConnection();
      connection.ConnectionString = connectionString; connection.Open();
      WriteObject(connection);
    }

  } // class
} // namespace

xUnit测试(包括对项目的引用)按预期工作。

将模块导入pwsh会话(在OS X上)后,我尝试使用Cmdlet:

$ pwsh
PowerShell 6.1.2
Copyright (c) Microsoft Corporation. All rights reserved.

PS [project]/bin/Debug/netcoreapp2.1> import-module PsOracleCore
PS [project]/bin/Debug/netcoreapp2.1> new-session
Could not load file or assembly 'Oracle.ManagedDataAccess, Version=2.0.18.3, Culture=neutral, PublicKeyToken=89b483f429c47342'. The system cannot find the file specified.
At line:1 char:1
+ new-session
+ ~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], FileNotFoundException
+ FullyQualifiedErrorId : System.IO.FileNotFoundException

我能够通过在项目的/bin/Debug/netcoreapp2.1目录中创建到程序集的符号链接来消除该错误:

$ ln -s ~/.nuget/packages/oracle.manageddataaccess.core/2.18.3/lib/netstandard2.0/Oracle.ManagedDataAccess.dll Oracle.ManagedDataAccess.dll

我的项目中缺少什么吗?

如果我想分发Cmdlet的程序集,我该怎么做才能确保正确安装和引用NuGet软件包?

1 个答案:

答案 0 :(得分:0)

我需要添加:

<PropertyGroup>
  <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

到我项目的.csproj文件中。

结果:

enter image description here

问题:

  • 是否可以将选定的程序集标记为copylocal而不是全部?鉴于此程序集是通过PowerShell引用的,因此System.*程序集似乎是不必要的。
  • 使用我的程序集时,NuGet是否应该处理依赖项的安装,而不必手动分发Oracle.ManagedDataAccess.dll程序集?