c# - 从 asp.net 核心应用程序启动 linux 进程失败(找不到文件)
问题描述
我正在使用 Windows 上的 Visual Studio 在 linux docker 容器中运行的 asp.net 核心中创建一个应用程序。这个应用程序根据 Process.Start() 所在的平台启动不同的进程。目前,该进程在我的本地 Windows 机器上运行时正确启动,但是当我切换到 linux 容器时,我收到此错误(即使我尝试启动的两个文件都存储在同一目录中)。我对 File.Exists(processPath) 进行了检查,它表明该文件确实存在,但是当启动该进程时,Interop.Sys.ForkAndExecProcess() 方法实际上似乎抛出“没有这样的文件或目录”尝试启动二进制文件。
Unhandled Exception: System.ComponentModel.Win32Exception: No such file or directory
at Interop.Sys.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Boolean setUser, UInt32 userId, UInt32 groupId, Int32& lpChildPid, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean shouldThrow)
at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start()
这是代码
var assemblyFileInfo = new FileInfo(typeof(TemplateClass).Assembly.Location);
var rootDirectory = Path.Combine(assemblyFileInfo.DirectoryName, "HelmExecutables/Data/");
var processPath = "";
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
processPath = Path.Combine(rootDirectory + "helm_windows.exe");
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
processPath = Path.Combine(rootDirectory + "helm_linux.out");
}
var process = new Process();
var startInfo = new ProcessStartInfo();
startInfo.FileName = processPath;
process.StartInfo = startInfo;
process.Start();
解决方案
在查看您的代码时,我想到的一件事是,如果两者都是,则processPath
可能只是一个空字符串。RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
false
我要做的是检查代码是否RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
在true
docker 映像中运行。
之后,我会Console.WriteLine(processPath)
(或以任何其他方式获取 的值processPath
)并尝试从命令行手动启动该可执行文件,看看会发生什么。
推荐阅读
- python - 在 Django 中使用原始 SQL 打开事务是安全的
- python - Python - 从其他模块中的函数重新分配全局变量的最佳方法
- javascript - 如何在 PHP 中翻转字节数组
- django - NGINX docker-compose - 在上游 nuxt:3000 中找不到主机
- android - Android Studio 上未将手机检测为 Android 设备
- node.js - AZURE 不允许使用 HTTP 错误 405 方法
- android - 在 Android Studio 中 Flutter 运行失败原因:java.lang.IllegalStateException:缺少 Crashlytics 构建 ID
- javascript - 在 html 网页上显示列表 (JSON)
- python - 错误 FileNotFoundError [winError 2] anaconda
- azure - Azure CLI 无法连接到 Docker 守护程序