c# - 使用 SimpleInjector 的二进制 PowerShell Core cmdlet 导致 FileNotFoundException
问题描述
问题
我正在尝试创建一个使用 SimpleInjector 的 PowerShell Core 二进制 cmdlet 库,但无法弄清楚为什么在FileNotFoundException
使用 Visual Studio 对其进行调试时会导致错误。
细节
我在 Visual Studio 中创建了一个超级简单的 PowerShell cmdlet 库,面向 .NET Standard 2.0 并使用PowerShellStandard.Library
包 (v5.1.0-RC1)。您可以在此处查看/克隆它。它包含以下 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,或者是否还有其他因素在起作用。如果有人能启发我,我将不胜感激!
解决方案
我不确定这是最好的解决方案,但我终于想出了解决我的问题的方法:
首先,我添加了一个构建后事件:dotnet publish --no-build
.
其次,在项目的“调试”选项卡中,我将“应用程序参数”更改为从\bin\Debug\netstandard2.0\publish\
目录导入我的 cmdlet 模块,而不是从\bin\Debug\netstandard2.0\
. 所以,而不是这个:
-NoExit -NoLogo -NoProfile -Command "Import-Module .\MyNetStandardProject.dll"
我用这个:
-NoExit -NoLogo -NoProfile -Command "Import-Module .\publish\MyNetStandardProject.dll"
如此处所示:
一旦我这样做了,我就可以运行Test-SimpleInjectorDependency
我的问题中描述的 cmdlet 而不会出错。
推荐阅读
- macos - 在 macos catalina 上安装 docker
- python - 数据库表的python内存问题
- android - NavHost 中可组合之间的 ExitAnimation
- javascript - 在将图像发送到 Tesseract OCR 之前进行图像预处理
- c# - C#:Enumerable.Range with except 返回超出范围的数字
- azure-active-directory - 如何在开发人员租户中使用单租户应用程序注册在客户租户中添加企业应用程序
- python - 在 csv 文件中添加一列,列值作为它所在的“文件夹”的名称
- javascript - 如何在不使用 JSX 的情况下编写 ReactJS 函数组件?
- python - 如何使用 Beautiful Soup 从 href 标签下的标签中提取字符串?
- office365 - OneNote 目前无法同步您的笔记。我们会继续努力。(错误代码:0xE0000007 9wgyi)