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

荒凉一梦 提交于 2019-12-22 22:46:36

问题


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.

I simply want to call an external .exe from a button on my page.

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

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

    success: function () {

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

回答1:


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. 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.

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


来源:https://stackoverflow.com/questions/48296629/launch-external-process-exe-from-asp-net-core-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!