首页 > 解决方案 > 从 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();

标签: c#linuxdockerasp.net-core.net-core

解决方案


在查看您的代码时,我想到的一件事是,如果两者都是,则processPath可能只是一个空字符串。RuntimeInformation.IsOSPlatform(OSPlatform.Windows)RuntimeInformation.IsOSPlatform(OSPlatform.Linux)false

我要做的是检查代码是否RuntimeInformation.IsOSPlatform(OSPlatform.Linux)truedocker 映像中运行。

之后,我会Console.WriteLine(processPath)(或以任何其他方式获取 的值processPath)并尝试从命令行手动启动该可执行文件,看看会发生什么。


推荐阅读