7

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");
    }
});
SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
Zoinky
  • 4,083
  • 11
  • 40
  • 78
  • what permissions would be required to run an external app? – Zoinky Jan 17 '18 at 12:14
  • 2
    Part of the problem here is Calc. When everything went UWP calc went with it, and you can't start a UWP process from a non-interactive session like the one the app pool starts under. So ... does it work with the actual process you're trying to start? (Not that I'd encourage this in any way) – blowdart Jan 17 '18 at 19:17

2 Answers2

4

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();
}
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 5
    'There are several, far more robust, architectural patterns for dealing with external processes outside your request pipeline.' Can you name one or two? – Riscie Mar 16 '18 at 13:01
  • 3
    It depends on the process. Generally, I lean towards a variation of either [Scheduler Agent Supervisor](https://learn.microsoft.com/en-us/azure/architecture/patterns/scheduler-agent-supervisor) or [Competing Consumers](https://learn.microsoft.com/en-us/azure/architecture/patterns/competing-consumers). The gist being that your main process only queues a request for the external process to run rather than having a direct dependency on it. – Marc LaFleur Mar 16 '18 at 13:49
  • @MarcLaFleur Any links to actual implementations? I understand the concept but I'm not sure how I can use it in real life to actually launch an exe.. – Inception Oct 22 '20 at 13:09
0

AzureWebJob is one of those implementations, not as simple, but it gets the job done