繁体   English   中英

从asp.net核心应用程序启动Linux进程失败(找不到文件)

[英]Launching linux process from asp.net core application fail (file not found)

我正在使用Windows上的Visual Studio在asp.net核心中创建一个在Linux docker容器中运行的应用程序。 此应用程序根据使用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();

如果RuntimeInformation.IsOSPlatform(OSPlatform.Windows)RuntimeInformation.IsOSPlatform(OSPlatform.Linux)都为false那么在您的代码中想到的一件事情就是processPath可以是一个空字符串。

我要做的就是检查代码在RuntimeInformation.IsOSPlatform(OSPlatform.Linux)映像中运行时RuntimeInformation.IsOSPlatform(OSPlatform.Linux)是否为true

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM