While trying to implement asyncronous email over smtp in my ASP.Net MVC 3 application I've come around SO SmtpClient.SendAsync blocking my ASP.NET MVC Request thread. There I found the article by Phil Haack: The Dangers of Implementing Recurring Background Tasks In ASP.NET which provides a way to avoid crashing of the background thread on AppDomain shutdown.
The article says to call HostingEnvironment.RegisterObject(this);
on the constructor and call HostingEnvironment.UnregisterObject(this);
only if IRegisteredObject.Stop Method is called.
In a general scenario, when requests arrive permanently, and the scope of the object implementing IRegisteredObject
is request, dosn't this approach register objects within each request (utilizing the email functionality) and does not unregister any?
Is it OK? Or should I also unregister after the asynchronous operation completed?
P.S.: as suggested by Damian Edwards in the linked SO question, I use ThreadPool.QueueUserWorkItem
to send the email beyond request scope.