Search code examples
performanceasynchronousasp.net-web-apiprocesssystem.diagnostics

What are some best practices when calling external executable from ASP.NET WEB API 2


I am in need to call an external *.exe compiled in C++ from ASP.NET WEB API 2 using Process (System.Diagnostics)

This executable does some image processing stuff and use lot of memory.

SO my question is if change my API calls to Async. or implement threads will it help, Or it doesn't matter?

Note: All i have is executable so i can not go for a CLI Wrapper.


Solution

  • You can separate the two. Your api is one thing, it needs to be fast, responsive to be able to serve the clients. Your image processing thing is different.

    You could implement a queuing system. The api is responsible for adding a new item to this queue and nothing more. You could keep track of what tasks are being run in a separate sql table let's say. Imagine you have a sql table called Tasks. Your api chucks data in there and the status is "Not Running".

    Some other app which lives on another machine entirely keeps an eye on this table and takes care of running that executable for each item. When it starts, it changes the status to Running, when it completes it's Done. You do whatever else you need. You could have an api endpoint which takes the ID of the task so your client can keep calling this endpoint to see what the status is. Or you could raise an event when it's done, depending on your application needs.

    Bottom line, keep things separate, you gain nothing for blocking the api while a resources heavy task is running. Think what happens if you start that process 5 times, at the same time. You've just killed your api basically.

    The app that does the heavy work, could even be located on a separate machine, so it doesn't affect the api at all.