代码之家  ›  专栏  ›  技术社区  ›  Nick Spreitzer

使用SimpleInjector的二进制PowerShell Core Cmdlet导致FileNotFoundException

  •  3
  • Nick Spreitzer  · 技术社区  · 7 年前

    问题

    我正在尝试创建一个使用SimpleInjector的PowerShell核心二进制Cmdlet库,但无法找出它为什么会导致 FileNotFoundException 使用Visual Studio调试时。

    细节

    我在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!");
        }
    }
    

    结果是 文件未找到异常 :

    simple-injector

    这个 似乎 特定于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!");
        }
    }
    

    json.net

    我真的不明白这里发生了什么。我甚至不确定这是否真的是特定于单工注射器,或者是否有其他因素在起作用。如果有人能启发我,我会非常感激的!

    1 回复  |  直到 7 年前
        1
  •  2
  •   Nick Spreitzer    7 年前

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

    首先,我添加了一个生成后事件: dotnet publish --no-build .

    enter image description here

    其次,在项目的“调试”选项卡中,我更改了“应用程序参数”以从 \bin\Debug\netstandard2.0\publish\ 目录,而不是来自 \bin\Debug\netstandard2.0\ 。所以,不要这样:

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

    我用过这个:

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

    如图所示:

    debug settings

    一旦我这样做,我就可以运行 Test-SimpleInjectorDependency 我的问题中描述的Cmdlet没有错误。

    推荐文章