HttpHandler isReusable, true or false for sending ~75 emails at a time
I'm trying to get my head around this isReusable
property of the IHttpHandler
interface but need your help.
I'm raising an event each time a document is saved in a CMS I'm using, and I want that event to fire off an action to a HttpHandler that would send emails to users subscribed to that document. There will be roughly 75 emails, and using my email delivery service (Postmark) that takes about 1-5 seconds per email, so a total of about 2-5 minutes.
开发者_如何转开发The handler will receive only 1 parameter (the document ID) and will then figure out who to send emails to and of course send them.
In this scenario, is it wise to set the isReusable property to true? And why?
Much appreciated.
If you just want to implement your own HttpHandler using the IHttpHandler interface you have to implement a function and a property of the interface. The function ProcessRequest() is used to implement your own custom request handler which returns the response according to your code.
IsReusable is for pooling of the handler objects. So if your handler is not holding any request specific state then you can return true to ask that it be pooled. This is typically done when your handler does very expensive initialization, otherwise it probabaly doesn't matter if you return true or false (since simple object allocation is fairly inexpensive in .NET). Pages are never pooled, BTW.
A simple test is for IsReusable to return true if the handler is thread safe. So as Adam pointed out the handler should be stateless.
Just a point on the long-running process of sending e-mails. You will be chewing up threads on the web server --- which is something you may not want to do to keep your site(s) responsive.
If at all possible you could consider a service bus and hand off the work to a separate process by doing so. e.g.:
bus.Send(new SendEMailCommand { id = theId });
This will queue your e-mail requests allowing you to control how many concurrent threads you allow for this processing --- threads that are running outside IIS in their own AppDomain :)
Just a thought.
精彩评论