Certain actions that my web site's users carry out result in the sending of emails. The code that sends emails can block for a while, so I want to do this off their HTTP request handler's thread.
Currently I'm using something like:
ThreadPool.QueueUserWorkItem(o => {
try
{
email.Send();
}
catch (Exception ex)
{
_log.Error("Error sending email", ex);
}
});
For the most part, this works. However the web site runs in a hosted environment where the app pool can be recycled.
Every once in a while I don't receive an email that should have been sent, and I suspect that this work item on the threadpool's queue is being dropped during application pool recycling.
How can I perform an ansync operation like this and guarantee that it will complete in such a case?