从 ASP.NET Core 应用程序启动外部进程 (.exe)

Zoi*_*nky 7 iis asp.net-core-mvc kestrel-http-server asp.net-core asp.net-core-2.0

我有以下内容

[HttpPost]
public IActionResult LaunchExternalProcess()
{
    Process.Start("C:\\Windows\\System32\\calc.exe");
    return Ok();

}
Run Code Online (Sandbox Code Playgroud)

这在我的本地计算机上运行得很好,但是当部署到 IIS 10(Windows 2016)上时,我没有收到任何错误,但它不会在服务器上启动计算。

我只想通过页面上的按钮调用外部 .exe。

这是我正在使用的 javascript,它也适用于我的本地,但在服务器上没有错误,并且显示成功消息

$.ajax({
    url: "/Admin/LaunchExternalProcess",
    type: "POST",
    cache: false,

    success: function () {

        console.log("success");
    }
});
Run Code Online (Sandbox Code Playgroud)

Mar*_*eur 4

首先,启动这样的外部流程是一个非常糟糕的主意。所以请不要在实际应用程序中这样做。您很可能会造成更多的问题和安全漏洞,而这是值得的。有几种更强大的架构模式可用于处理请求管道之外的外部流程。

也就是说,这里的问题是calc.exe无法在您的服务器上启动。您的方法不知道这一点,但是因为您只是告诉它启动 a Process,所以您没有检查该进程处于什么状态。

var process = Process.Start("C:\\Windows\\System32\\calc.exe");
if (process == null) // failed to start
{
    return InternalServerError();
}
else // Started, wait for it to finish
{
    process.WaitForExit();
    return Ok();
}
Run Code Online (Sandbox Code Playgroud)

  • “有几种更强大的架构模式可用于处理请求管道之外的外部流程。” 你能说出一两个吗? (5认同)
  • 这取决于过程。一般来说,我倾向于 [调度程序代理主管](https://learn.microsoft.com/en-us/azure/architecture/patterns/scheduler-agent-supervisor) 或 [竞争消费者](https://learn.microsoft.com/en-us/azure/architecture/patterns/scheduler-agent-supervisor) 的变体/learn.microsoft.com/en-us/azure/architecture/patterns/competing-consumers)。要点是您的主进程仅对外部进程运行的请求进行排队,而不是直接依赖它。 (4认同)