【发布时间】:2019-07-09 01:14:17
【问题描述】:
我创建了一个 C# 类库,它将托管许多 PowerShell Core (6.1.2) Cmdlet。
该项目包含两个 NuGet 包:
- System.Management.Automation(用于核心)(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 包?
【问题讨论】:
-
你的模块文件夹在磁盘上是什么样子的?我只在 Windows 上工作(使用框架,而不是核心),但是当我构建和部署时,模块文件夹包括我的 DLL,DLL-Help.xml 文件(如果你不使用 RedGate 的 XmlDoc2CmdletDoc 包,看看它)以及 C# 代码使用的所有 NuGetted 内容。 NuGet-ted DLL 不会被“安装”,它们只是被复制到输出 bin 文件夹(然后复制到模块文件夹)
标签: c# powershell powershell-core