代码之家  ›  专栏  ›  技术社区  ›  craig

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

  •  0
  • craig  · 技术社区  · 6 年前

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

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

    • 系统经营自动化(核心)(6.1.2)
    • 神谕ManagedDataAccess。核心(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软件包?

    0 回复  |  直到 6 年前
        1
  •  0
  •   craig    6 年前

    我需要补充一点:

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

    给我的项目的 .csproj 文件

    结果是:

    enter image description here

    问题:

    • 是否可以将选定的程序集标记为copylocal,而不是所有程序集?鉴于此程序集是通过PowerShell引用的 System.* 这些似乎没有必要。
    • 当使用我的程序集时,NuGet不应该处理依赖项的安装,从而消除分发 Oracle.ManagedDataAccess.dll 手工组装?