从asp.net核心应用启动外部进程(.exe) [英] Launch external process (.exe) from asp.net core app

查看:790
本文介绍了从asp.net核心应用启动外部进程(.exe)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容

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

}

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

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.

我只是想从页面上的按钮调用外部.exe.

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

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

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

推荐答案

首先,启动这样的外部进程是一个非常糟糕的主意.因此,请不要在真实的应用程序中这样做.您很有可能会创建更多值得拥有的问题和安全漏洞.有几种更健壮的架构模式可用于处理请求管道之外的外部流程.

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.

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

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

这篇关于从asp.net核心应用启动外部进程(.exe)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆