What is the best way to implement background services for an ASP.NET application?

My ASP.NET application needs several helper services to run in the background periodically. For example:

  • I need to query the database (or cache) every 1-5 minutes, identify expired work items, and notify users via email.
  • I need to generate nightly reports, which are then emailed to subscribers
  • Plus other system / non-user admin tasks

What is the best way to implement these services? Should I include them as part of the web application by running an instance of each "service" in Application_Start ()? Or create them like real autonomous services? If they were part of a web application, I can potentially use the data store in the cache, but are there any drawbacks to this approach?

Thanks for any advice.

+6
design architecture deployment
source share
5 answers

This year I had to perform one task to cache some data from the database, she had to check the changing code every n minutes, I found this very nice article;

Simulate a Windows service using ASP.NET to run scheduled tasks

I did it this way and it worked, but as I said, it was only one task, but if you see the example above, you will see that it performs several tasks.

As you can see, in the cache you can perform the task at any time and at intervals, the night task can be performed at midnight, the rest every n minutes or hours.

Hope this helps.

+6
source share

- For the nightly generation of reports, I would look at SQL Reporting Services, because they have some of their own subscription services that allow you to send e-mail messages to users without virtual code.

- For notifications of expired work, you can use SSRS (mentioned above) by sending them a report with expired goods or, if you use SQL 2k5, use notification services.

+1
source share

I deal with production threads and threads that run periodically in my ASP.NET application and I had no problems.

I do not just run the application in the house - others download it and run it on their sites, or run it on hosting companies, so doing something without requiring the installation of a service makes deployment easier.

+1
source share

The timer combined with the background worker worked fine for me:

Timer timer = new Timer(intervalSeconds * 1000) timer.Elapsed += delegate { // do the meat }; timer.Start(); 

Keep in mind that you will not have access to the HttpContext and that the site may be down due to inactivity.

+1
source share

Honestly, I have never been a fan of trying to mimic a Windows service inside an ASP.net process, especially considering how easy it is to create a Windows service. Most of the attempts that I saw at the same time were full of problems.

0
source share

All Articles