The best way to handle this would be to have a separate service to actually perform the tasks that you want to run as opposed to running them from within your web api controllers.
Have the Web API endpoint write an entry to the database requesting some work to be done and have another separate service read from that table and perform those long running tasks.
You can of course provide some kind of id so that you can query for progress using another end point within your WEB API if you so wish.
The reason for this and the problem you are likely going to run into is that ASP.NET only knows about code running that is associated with a request, the code you are looking to run in a background thread would not be seen as part of a request could therefore be ended at any point for a number of reaons...
Here are some examples of those risks:
- An unhandled exception in a thread not associated with a request will take down the process.
- If you run your site in a Web Farm, you could end up with multiple instances of your app that all attempt to run the same task at the same time.
- The AppDomain your site runs in can go down for a number of reasons and take down your background task with it. This could corrupt data if it happens in the middle of your code execution.
See this page for more information:
http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx/
Hope this helps.