[英]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.