我正在使用 Windows 上的 visual studio 在 asp.net core 中创建一个在 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();
最佳答案
在查看您的代码时,我想到的一件事是 processPath
可以只是一个空字符串,如果 RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
和 RuntimeInformation .IsOSPlatform(OSPlatform.Linux)
为 false
。
当代码在 docker 镜像中运行时,我要做的是检查 RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
是否为 true
。
之后,我将Console.WriteLine(processPath)
(或以任何其他方式获取processPath
的值)并尝试从命令启动该可执行文件-手动排队,看看会发生什么。
关于c# - 从 asp.net 核心应用程序启动 linux 进程失败(找不到文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57487227/