Best ASP.NET Background Service Implementation
Asked Answered
D

3

1

What's the best implementation for more than one background service in an ASP.NET application?

  1. Timer Callback

    Timer timer = new Timer(new TimerCallback(MyWorkCallback), HttpContext, 5000, 5000);
    
  2. Thread or ThreadPool

    Thread thread = new Thread(Work);
    thread.IsBackground = true;
    thread.Start();
    
  3. BackgroundWorker

    BackgroundWorker worker = new BackgroundWorker(); 
    worker.DoWork += new DoWorkEventHandler(DoMyWork);
    worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(DoMyWork_Completed); 
    worker.RunWorkerAsync();
    
  4. Caching like http://www.codeproject.com/KB/aspnet/ASPNETService.aspx (located in Jeff Atwood's post here)

I need to run multiple background "services" at a given time. One service may run every 5 minutes where another may be once a day. It will never be more than 10 services running at a time.

D answered 15/9, 2009 at 2:27 Comment(0)
C
2

Well, instead of a 'Simple Thread', you'd go for a ThreadPool.

And if it were me, I'd run it in a Windows Service and communicate to it via MSMQ.

Charters answered 15/9, 2009 at 2:31 Comment(3)
I'd run a Windows Service instead as well, but if you are not sure if the end user has that ability for example in a shared hosting envoironment, I want to still be sure I can still host these "services."D
I edited my Simple Thread tag to ThreadPool. I also found a fourth option - codeproject.com/KB/aspnet/ASPNETService.aspx. Thanks again for your response!D
Regarding that option; it was brought to my attention that it will only work if someone is hitting the site. If no-one visits, it may not remove the item from the cache and execute the task. I'd check it through myself (I haven't) before using it. Certainly Windows Services is the most stable, if it exists as an option.Charters
M
0

You should implement some states of tasks. After application recycles background service should repair broken tasks. If tasks are under transactions then there will be no problems with that behaviour.

Marketplace answered 30/9, 2009 at 11:40 Comment(0)
U
0

None of the suggestions of the original poster scales to more than one server.

I want the same thing: A robust and simple to use way of scheduling regular background tasks and perform ad-hoc heavy processing (rebuild caches and so on) without affecting the current users request.

I want to have the tasks run in web apps context, to a) get the config from web.config (avoid duplication of config) and b)be deployed with the web app (ease deployment). And I need it to work for websites spread to more than one server.

I've only found one approaches that provides somewhat what I want:

(1) Create an .aspx page for each task and setup a wget scheduled task on one separate server that calls this .aspx on the website. I have implemented this approach and it works, but I do not like it 100%, because of the scheduled task dependency and it does not give me the ability to perform heavy tasks ad hoc in the background

Untaught answered 20/4, 2010 at 18:42 Comment(1)
For a service to run on multiple servers, I'd strongly recommend breaking that out into a Windows Service that interacts with a message queue of some sort like MSMQ. That would be your best bet. What I was looking for was something for a shared hosting environment and not for a self hosted or virtual hosted environment.D

© 2022 - 2024 — McMap. All rights reserved.