Binary PowerShell Core cmdlet using SimpleInjector results in FileNotFoundException

Viewed 319

The Question

I'm trying to create a PowerShell Core binary cmdlet library that uses SimpleInjector, but cannot figure out why it's resulting in a FileNotFoundException when debugging it with Visual Studio.

Details

I've created a super simple PowerShell cmdlet library in Visual Studio, targeting .NET Standard 2.0 and using the PowerShellStandard.Library package (v5.1.0-RC1). You can view/clone it here. It contains the following 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!");
    }
}

The result is a FileNotFoundException:

simple-injector

This seems specific to SimpleInjector because doing the same thing with, for example, json.net runs without a problem:

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

I really don't understand what is happening here. I'm not even sure if this is truly specific to SimpleInjector or if there are other factors at play. If anyone can enlighten me, I'd really appreciate it!

1 Answers

I'm not sure this is the best solution, but I did finally come up with a resolution to my problem:

First, I added a post-build event: dotnet publish --no-build.

enter image description here

Second, in the project's "Debug" tab, I changed the "Application arguments" to import my cmdlet module from the \bin\Debug\netstandard2.0\publish\ directory, instead of from \bin\Debug\netstandard2.0\. So, instead of this:

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

I used this:

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

As shown here:

debug settings

Once I did that, I was able to run the Test-SimpleInjectorDependency cmdlet described in my question without error.

Related