In my ASP.NET MVC 4 application, say I have some async
method:
async Task DoSomeBackgroundWork()
{
// Fake it
await Task.Delay(5000);
}
Now, one can call this method like this:
Task.Run(async () =>
{
await DoSomeBackgroundWork();
Debug.WriteLine("Background work finished");
});
or this:
DoSomeBackgroundWork()
.ContinueWith(t => Debug.WriteLine("Background work finished"));
Note that in both versions the caller doesn't wait for the background task to finish. It's just fire and forget.
While both versions work identical (ignore exception handling here) on regular .NET apps, on a MVC app, when calling the code from a MVC controller method, Debug.WriteLine
is only executed in the Task.Run
version - but not in the ContinueWith
version.
Can anyone shed some light onto why these two versions are working differently when called in a MVC controller method?