Member-only story
Run an External Executable in ASP.NET Core
How to execute an external program by calling an API endpoint.

Sometimes our application needs to allow users to execute an external program by calling an API endpoint. The external program could be a script, a third-party software, or an executable written in an outdated framework.
In order to trigger a background process that cannot be easily integrated in the modern framework or infrastructure, generally we want to decouple the systems. For instance, we can let our system delegate the external process to a job management agent, e.g., Jenkins. Another common approach is to fire a command in a message queue, then the external executable subscribes the event and takes actions accordingly.
Life happens. When we are not able to afford to spin up a set of infrastructure, we have to let our application call a command line interface directly. This post will go over an example use case that a helloworld.exe
(in Windows) is executed from an ASP.NET Core Web API endpoint. This solution might need to be slightly modified if your application works in another OS. The full solution can be found in this GitHub repository.
Show me the code
We first create a contrived program called helloworld.exe
in .NET framework. This helloworld.exe
simulates the external program that will be triggered by an ASP.NET Core Web API project. This program, see the code snippet below, simply writes some logs to the Console and creates a file on disk.
We build the project to generate a helloworld.exe
file and we know its location.
Then we create a bare-bones ASP.NET Core Web API project. For demo purposes, we modify the Get(int id)
action method in ValuesController
class to be the following.