Reputation: 8729
I am creating an Intranet webpage for my office.
The application needs to download SMTP email every 5 minutes. (As well as some other house keeping things)
I have just created a ASP.NET webpage that is running on IIS through Windows 2008 R2 server.
Do I have to create a seperate Windows Service to do this? So I would just have 2 programs, the webpage and the windows service?
or is there a better way to do it?
Upvotes: 4
Views: 5050
Reputation: 536
For me the solution entailed a console application and a Window Service. I have a windows service that simply starts console applications if they aren't running. It looks ever 5 seconds to see if any of the applications (stored as registry keys so I can update the list of applications without having to rewrite the service) are running, if they aren't, then they are started. This also makes debuging the code easier because I don't have to remember how to attach, etc, to debug the windows service every time but simply debug the console applicaiton. The console application has all the necesary code to connect to the DB and do what it needs, in my case compare values and then send alarm emails if necessary.
Upvotes: 0
Reputation: 1
Easier way is to create a console application and use Task Scheduler to run it in every 5 min
Upvotes: 0
Reputation: 1249
The simplest way to do recurring stuffs on an asp.net web site is
System.Timers.Timer
object and keep it reference on a static field in your code (to prevent garbage collection)Elapsed
event and put your recurring tasks therepublic class Global : System.Web.HttpApplication
{
private static Timer timer;
protected void Application_Start(object sender, EventArgs e)
{
timer = new Timer(5 * 60 * 1000);
timer.AutoReset = true;
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
}
void timer_Elapsed(object sender, ElapsedEventArgs e)
{
//Get our emails here
}
}
It does not matter:
If it's really important, know that by default, an app pool is configured to recycle just once a day and to not stop even if there are no traffic.
Upvotes: 5
Reputation: 1889
I think Windows service is the best way. Other than this you may be able to set the IIS App Pool not to recycle and start some worker threads that will perform the task on a specified time interval.
Choosing the second will probably be better once you get it right but it will have to do with the IIS infrastructure so you will need set up the IIS each time you deploy your application on a new server.
If someone doesn't suggest something more clever - I will suggest Windows Service (be careful when deploying it and be sure to set CanStop to true to avoid the need to restart the server at a later deployment :) and some medium (IPC communication, DB, simple file / xml, WCF Service on the Windows Service side or MSMQ) to exchange data between the Web App and the Windows Service.
So to answer your question - you can do it both ways theoretically but the Windows Service seems to be more reliable and safe (you won't need to somehow instruct IIS to never kill your Web Process).
Upvotes: 0