11

I have an ASP.NET MVC 3 web app hosted on normal hosting, (i.e., no owned or virtual server), and I would like to be able to store e-mails say in a database and have them picked up and sent in a background job of sorts.

If this was my own server I'd write a Windows Service to handle this, but is there any way I could implement/simulate a scheduled task/background job in a web application process?

Nate
  • 30,286
  • 23
  • 113
  • 184
Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101

3 Answers3

9

You may take a look at Quartz.NET. There is a blog post about how you could make it running in medium trust.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
9

You can take a look at this post (https://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/) which explains on how run scheduled tasks without using windows service, in asp.net or asp.net mvc.

Community
  • 1
  • 1
sarvesh
  • 2,743
  • 1
  • 20
  • 30
  • Hey sarvesh, I had read the post a while ago, but I'm a bit weary of going the same way. I don't know, wouldn't I clog up the app if I sent hundreds of e-mails from a cache-started job? – Sergi Papaseit Mar 01 '11 at 21:25
  • It won't hold your asp.net requests while your emails are being sent. How often will you be sending these emails? If you are worried about the load on your server can you send them in small batches? – sarvesh Mar 01 '11 at 21:46
  • @sarvesh: relying on asp.net to host a recurring code execution (as per article you provided) is not reliable - if your asp.net application was idle for a certain period of time (no users, no http requests), it WILL recycle all threads it created and recycle itself until the next request of any of its resources. In general, cron or Quartz.net would be ideal to achieve what Sergi is trying to achieve. But in the case of "limited" hosting the only real option is to use one of online services that make periodic http requests to user's web page or sproc based on the schedule that the user sets. Th –  Mar 02 '11 at 02:10
1

You can also use job cash:

 HttpContext.Current.Cache.Add("jobkey",  
                         "jobvalue", null,  
                         DateTime.MaxValue,  
                         TimeSpan.FromSeconds(15), // set the time interval  
                         CacheItemPriority.Default, JobCacheRemoved); 

For Details See this

Mohammad Atiour Islam
  • 5,380
  • 3
  • 43
  • 48