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

标签 c# linux docker asp.net-core .net-core

我正在使用 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/

相关文章:

java - rabbitmq,Java 客户端通过 SSL 工作,但 .NET 客户端不能

c# - 如何以编程方式选择 GridView 行以便触发 "OnSelectedIndexChanged"事件

python - Linux 上的 python httplib2 问题

linux - Netcat HTTP 发布

macos - 是否有在 Docker for Mac 中使用主机网络的解决方法?

c# - 语言转换测试

c# - Newtonsoft.JSON Mono Float反序列化异常

linux - 将 stat 命令的输出与 shell 脚本中的整数变量进行比较

docker - 在 ubuntu 中限制在 docker 上运行的端口

spring - 容器化 Spring Boot 应用程序时 "-Djava.security.egd=file:/dev/./urandom"到底做了什么