简体   繁体   English

从 asp.net core 应用程序启动外部进程 (.exe)

[英]Launch external process (.exe) from asp.net core app

I have the following我有以下

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

}

And this works perfectly fine on my local machine, but when deployed onto IIS 10 (windows 2016) I get no errors but it does not launch the calc on the server.这在我的本地机器上运行得非常好,但是当部署到 IIS 10(windows 2016)上时,我没有收到任何错误,但它不会在服务器上启动计算。

I simply want to call an external .exe from a button on my page.我只想从页面上的按钮调用外部 .exe。

Here is the javascript that I am using which also works on my local but no errors on server and it displays the success message这是我正在使用的 javascript,它也适用于我的本地但在服务器上没有错误,它显示成功消息

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

    success: function () {

        console.log("success");
    }
});

First, it is a very bad idea to spin up an external process like this.首先,像这样启动外部进程是一个非常糟糕的主意。 So please, don't do this in a real application .所以请不要在真正的应用程序中这样做 You will more than likely create far more issues and security holes that it would ever be worth.您很可能会创建更多值得的问题和安全漏洞。 There are several, far more robust, architectural patterns for dealing with external processes outside your request pipeline.有几种更健壮的架构模式可用于处理请求管道之外的外部流程。

That said, the problem here is that calc.exe is failing to launch on your server.也就是说,这里的问题是calc.exe无法在您的服务器上启动。 Your method doesn't know about this however since you're simply telling it to start a Process , you're not checking to see what state that process is in.您的方法不知道这一点,但是因为您只是告诉它启动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();
}

AzureWebJob 是其中一种实现,不是那么简单,但它可以完成工作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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