【问题标题】:Binary PowerShell Core cmdlet using SimpleInjector results in FileNotFoundException使用 SimpleInjector 的二进制 PowerShell Core cmdlet 导致 FileNotFoundException
【发布时间】:2018-10-21 21:32:11
【问题描述】:

问题

我正在尝试创建一个使用 SimpleInjector 的 PowerShell Core 二进制 cmdlet 库,但在使用 Visual Studio 调试它时无法弄清楚为什么它会导致 FileNotFoundException

详情

我在 Visual Studio 中创建了一个超级简单的 PowerShell cmdlet 库,面向 .NET Standard 2.0 并使用 PowerShellStandard.Library 包 (v5.1.0-RC1)。 You can view/clone it here。它包含以下 cmdlet:

using System.Management.Automation;
using Newtonsoft.Json.Linq;
using SimpleInjector;

[Cmdlet(VerbsDiagnostic.Test, "SimpleInjectorDependency")]
public class TestSimpleInjectorDependencyCmdlet : PSCmdlet
{
    protected override void ProcessRecord()
    {
        var container = new Container();
        WriteObject("Success!");
    }
}

结果是FileNotFoundException

似乎特定于 SimpleInjector,因为使用例如 json.net 执行相同的操作没有问题:

using System.Management.Automation;
using Newtonsoft.Json.Linq;
using SimpleInjector;

[Cmdlet(VerbsDiagnostic.Test, "JsonDotNetDependency")]
public class TestJsonDotNetDependencyCmdlet : PSCmdlet
{
    protected override void ProcessRecord()
    {
        var j = new JObject();
        WriteObject("Success!");
    }
}

我真的不明白这里发生了什么。我什至不确定这是否真的特定于 SimpleInjector,或者是否还有其他因素在起作用。如果有人能赐教,我将不胜感激!

【问题讨论】:

  • 这通常是构建或 NuGet 问题,但老实说,我不确定我是否会使用 DI 容器来构建我的 PowerShell 命令行开关。它们通常足够小,可以使用 Pure DI。
  • 无论这种方法是否是一个好主意,我仍然想了解正在发生的事情。称其为纯粹的好奇心。

标签: c# powershell .net-standard simple-injector powershell-core


【解决方案1】:

我不确定这是最好的解决方案,但我终于找到了解决问题的方法:

首先,我添加了一个构建后事件:dotnet publish --no-build

其次,在项目的“调试”选项卡中,我将“应用程序参数”更改为从\bin\Debug\netstandard2.0\publish\ 目录而不是\bin\Debug\netstandard2.0\ 导入我的cmdlet 模块。所以,而不是这个:

-NoExit -NoLogo -NoProfile -Command "Import-Module .\MyNetStandardProject.dll"

我用过这个:

-NoExit -NoLogo -NoProfile -Command "Import-Module .\publish\MyNetStandardProject.dll"

如下图:

一旦我这样做了,我就可以毫无错误地运行我的问题中描述的Test-SimpleInjectorDependency cmdlet。

【讨论】:

  • 谢谢 - 我遇到了类似的问题并用你的解决方案解决了 - 你是否进入了忽略 VS 中的断点(在 Cmdlet 代码中)的状态?
  • 不是在这种特殊情况下,不。但是当我过去发生这种情况时,通常是因为我加载了旧的二进制文件。确保您已加载最近编译的二进制文件,看看是否有帮助。
  • 好吧,出于某种原因,我不得不从命令行启动 powershell,然后使用“附加到进程”并选择 powershell。如果我通过右键单击 .net 标准库并单击 Debug 运行调试器,它不会遇到断点(在 VS 2017 和 2019 上尝试过)。我只在网上看到另一个人遇到了这个问题。
  • 我注意到出了什么问题,我使用的是“powershell.exe”(Windows 版本),而不是 Powershell 6,“核心”版本
  • 啊,有道理。很高兴你知道了!
猜你喜欢
  • 2017-02-15
  • 1970-01-01
  • 2023-03-24
  • 2017-07-19
  • 2017-10-16
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 1970-01-01
相关资源
最近更新 更多