First off, let me point out that “fire and forget” is almost always a mistake in ASP.NET applications. “Fire and forget” is only an acceptable approach if you don’t care whether DeleteFooAsync
actually completes.
If you’re willing to accept that limitation, I have some code on my blog that will register tasks with the ASP.NET runtime, and it accepts both synchronous and asynchronous work.
You can write a one-time wrapper method for logging exceptions as such:
private async Task LogExceptionsAsync(Func<Task> code)
{
try
{
await code();
}
catch(Exception exception)
{
m_log.Error("Call failed: " + exception.ToString());
}
}
And then use the BackgroundTaskManager
from my blog as such:
BackgroundTaskManager.Run(() => LogExceptionsAsync(() => DeleteFooAsync()));
Alternatively, you can keep TaskScheduler.UnobservedTaskException
and just call it like this:
BackgroundTaskManager.Run(() => DeleteFooAsync());