views:

639

answers:

5

I am writing a web application in ASP.NET 3.5 that takes care of some basic data entry scenarios. There is also a component to the application that needs to continuously poll some data and perform actions based on business logic.

What is the best way to implement the "polling" component? It needs to run and check the data every couple of minutes or so.

I have seen a couple of different options in the past:

  1. The web application starts a background thread that will always run while the web application does. (The implementation I saw started the thread in the Application_Start event.)
  2. Create a windows service that is always running

What are the benefits to either of these options? Are there additional options?

I am leaning toward a windows service because it is separated and can run on a different server (more scalable) as well as there is more control over when it is started/stopped, etc. However, I feel like the compactness of having the "background" logic running in the process of the web application might make the entire solution more understandable.

+4  A: 

I'd go for the separate Windows service primarily for the reasons you give:

  • You can run it on a different server if necessary.
  • You can start and stop it independently of the web site.

I'd also add that it could well have some impact on the performance of the web site itself - something you want to avoid.

The buzz-word here is "separation of concerns". The web site is concerned with presenting the data to the user, the service with checking the integrity of the data.

You can also update the web site and service independently of each other should you need to.

I was going to suggest that you look at a scheduled task and let Windows control when the process runs, but I re-read your question and noted that you wanted the checks to run every couple of minutes. The overhead of starting the process might be too great in this case - though some experimentation would probably prove this one way or the other.

There's the possibility that you could start the next check before the current one has finished - something you can code for if you're in complete control.

ChrisF
+2  A: 

Why not just use a console app that has no ui? Can do all that the windows service can and is much easier to debug and maintain. I would not do a windows service unless you absolutely have to.

jle
You could write and debug your console app and then just run it as a windows service, thereby having the best of both worlds.
Jim In Texas
I wanted to say this, but couldn't find the words.
ChrisF
You are still stuck with the problem of starting/stopping/installing/uninstalling every time that you want to make any changes... You can run a console app when a user is not logged in... only problem is running with elevated privileges (which a windows service can do)
jle
Not really a problem--you can set the identity of a scheduled task, so you can run with any privileges you could create.
Wyatt Barnett
A: 

Console application does not do well in this case. I wrote a TAPI application which has to stay in the background and intercept incoming calls. But it did it only once because the tapi manager got GCed and was never available for the second incoming call.

+1  A: 

You might find that the SQL Server job scheduler sufficient for what you want.

ebpower
That sounds like an idea. Please give more detail.
John Saunders
The data being polled is probably in a database anyway, so performing the poll check in the DB makes sense. Setting up a SQL Job is easier in a shared host than installing a windows service, and is always going to run unlike a ASP.NET background thread which will die with application when the app shuts down after 20 minutes of inactivity (depending on settings, etc). The SQL job could then call back into the site to perform the actions required if neccessary.
Zhaph - Ben Duguid
Excellent points!This link shows how to create a job:http://msdn.microsoft.com/en-us/library/ms186273.aspxThis link shows how to schedule a job:http://msdn.microsoft.com/en-us/library/ms191439.aspx
ebpower
And check out this link for a good discussion on Windows service vs SQL Server jobs: http://stackoverflow.com/questions/250518/windows-service-or-sql-job
ebpower
A: 

Hi,

I have a similar scenario. Can you tell me which solution worked best for you.

Thanks

Tanvi