I have a webapi that is designed to process reports in a queue fashion. The steps the application takes are as follows:
I like @Todd's solution. Just for the sake of completeness, but there's another option not mentioned yet:
HostingEnvironment.QueueBackgroundWorkItem(cancellationToken =>
{
// Some long-running job
});
Note:
"When ASP.NET has to recycle, it will notify the background work (by setting a CancellationToken) and will then wait up to 30 seconds for the work to complete. If the background work doesn’t complete in that time frame, the work will mysteriously disappear."
And avoid using service methods with an injected DbContext here as this won't work.
Sources: MariusSchulz and StephenCleary