Long running task in WebAPI

asp.net-web-api, async-await, task-parallel-library

Solution

Stephen described why starting essentially long running fire-and-forget tasks inside an ApiController is a bad idea.

Perhaps you should create a separate service to execute those fire-and-forget tasks. That service could be a different ApiController, a worker behind a queue, anything that can be hosted on its own and have an independent lifetime.

This would make management of the different task lifetimes much easier and separate the concerns of the long-running tasks from the ApiController's core responsibilities.

Problem

Here's my problem: I need to call multiple 3rd party methods inside an ApiController. The signature for those methods is `Task DoSomethingAsync(SomeClass someData, SomeOtherClass moreData)`. I want those calls to continue running in the background, after the ApiController has sent the data back to the client. When `DoSomethingAsync` completes I want to do some logging and maybe save some data to the file system. How can I do that? I'd prefer to use the asyny/await syntax.

Original source