简体   繁体   中英

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

I am creating an app in asp.net core that is run in a linux docker container using visual studio on windows. This app launches a different process depending on what platform it is on with Process.Start(). Currently, the process is launched correctly when run on my local windows machine, but when I switch to linux container I get this error (even tho both files I am attempting to launch are stored in the same directory). I did a check with File.Exists(processPath) and it shows that the file does in fact exist, but when the process is launched the Interop.Sys.ForkAndExecProcess() method seems to throw "No such file or directory" when it actually tries to launch the binary.

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()

Here is the code

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();

One thing that came into my mind looking your code is processPath can be just an empty string if both RuntimeInformation.IsOSPlatform(OSPlatform.Windows) and RuntimeInformation.IsOSPlatform(OSPlatform.Linux) are false .

What I would do is check if RuntimeInformation.IsOSPlatform(OSPlatform.Linux) is true when the code runs in the docker image.

After that, I would Console.WriteLine(processPath) (or get the value of processPath in any other way) and try to start that executable from the command-line manually and see what happens.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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