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?
If your application runs in integrated mode, you can register your mail dispatcher service within the host environment. The host will notify your service before a recycling is done.
The host will call your implementation of
IRegisteredObject.Stopexactly 2 times. On the first call the host gives you the opportunity to finish the job. If the timeout is reached and your service has not removed itself from the host, then another call is made, but this time only to notify that the recycling will be made with or without the consent of the service.This is an example (not tested) of how you can implement the Stop() method:
Start the service and register it on the host.