views:

164

answers:

6

We need the ability to send out automatic emails when certain dates occur or when some business conditions are met. We are setting up this system to work with an existing ASP.NET website. I've had a chat with one of the other devs here and had a discussion of some of the issues.

Things to note:

  • All the information we need is already modelled in the ASP.NET website
  • There is some business-logic that is required for the email generation which is also in the website already

We decided that the ideal solution was to have a separate executable that is scheduled to run overnight and do the processing and emailing. This solution has 2 main problems:

  • If the website was updated (business logic or model) but the executable was accidentally missed then the executable could stop sending emails, or worse, be sending them based on outdated logic.
  • We are hoping to use something like this to use UserControls to template the emails, which I don't believe is possible outside of an ASP.NET website

The first problem could have been avoided with build and deployment scripts (which we're looking into at the moment anyway), but I don't think we can get around the second problem.

So the solution we decided on is to have an ASP.NET page that is called regularly by SSIS and to have that do a set amount of processing (say 30 seconds) and then return. I know an ASP.NET page is not the ideal place to be doing this kind of processing but this seems to best meet our requirements. We considered spawning a new thread (not from the worker pool) to do the processing but decided that if we did that we couldn't use the page returned to signify a success or failure. By processing within the page's life-cycle we can use the page content to give an indication of how the processing went.

So the question is: Are there any technical problems we might have with this set-up?

Obviously if you have tried something like this any reports of success/failure will be appreciated. As will suggestions of alternative set-ups.

Cheers,

A: 

Sounds like you should be creating a worker thread to do that job.

Ariel
Why do you think processing in a separate thread would be better than processing in the page life-cycle? From my reading any threads you create can die whenever the application is recycled in the IIS app pool. So by keeping it within a page life-cycle we were hoping to avoid that happening.
David
A: 

Maybe you should look at something like http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/

Craig
I believe that this type of system will stop working if the AppPool is recycled (in a period of inactivity, or if the server is restarted). So if you wanted to ensure it is always running you'd need to have an externally-timed system requesting a page from your website. In which case you may as well have that external timer triggering the action directly (one less thing to go wrong). (http://www.codeproject.com/KB/aspnet/ASPNETService.aspx) suggests registering your site on a 3rd-party availability checker to ensure frequent requests. But I think that's a horrible solution.
David
+3  A: 

Don't use the asp.net thread to do this. If the site is generating some information that you need in order to create or trigger the email-send then have the site write some information to a file or database.

Create a Windows service or scheduled process that collects the information it needs from that file or db and run the email sending process on a completely seperate process/thread.

What you want to avoid is crashing your site or crashing your emailer due to limitations within the process handler. Based on your use of the word "bulk" in the question title, the two need to be independent of each other.

grenade
+1  A: 

I think you should be fine. We use the similar approach in our company for several years and don’t get a lot of problems. Sometimes it takes over an hour to finish the process. Recently we moved the second thread (as you said) to a separate server.

Estelle
+1  A: 

Having the emailer and the website coupled together can work, but it isn't really a good design and will be more maintenance for you in the long run. You can get around the problems you state by doing a few things.

  1. Move the common business logic to a web service or common library. Both your website and your executable/WCF service can consume it, and it centralizes the logic. If you're copying and pasting code, you know there's something wrong ;)

  2. If you need a template mailer, it is possible to invoke ASP.Net classes to create pages for you dynamically (see the BuildManager class, and blog posts like this one. If the mailer doesn't rely on Page events (which it doesn't seem to), there shouldn't be any problem for your executable to load a Page class from your website assembly, build it dynamically, and fill in the content.

This obviously represents a significant amount of work, but would lead to a more scalable solution for you.

womp
A: 

You can and should build your message body (templated message body) within domain logic (it means your asp.net application) when some business conditions are met and send it to external service which should only send your messages. All messages will have proper informations.

For "when certain dates occur" scenario you can use simple solution for background tasks (look at Craig answer) and do the same as above: parse template, build message and fast send to specified service.

Of course you should do this safe then app pool restarts does not breaks your tasks.

dario-g