Last post Dec 11, 2007 02:58 PM by joshelster
Dec 01, 2007 03:08 AM|flougr|LINK
Hello to everybody,
I have created a Web Application (using ASP.NET 2 & VB.NET) which i am going to host into a Shared Hosting Provider.
My Application has a request to process database items (Scheduled messages) in the background every 10 minutes , even if no users are active.
Looking around , i found an article which proposed to use a HttpModule with a Thread in order to do that.
Some colleagues told me instead , that i shoud put the Thread in the Global.asax file.
The code i use is the followig :
Public Class CSBkGroundWorker
Private _context As System.Web.HttpApplication
Public Sub Init(ByVal context As System.Web.HttpApplication) Implements System.Web.IHttpModule.Init
_context = context
Dim t As New Thread(AddressOf WriteLog)
Public Shared Sub WriteLog()
CustomNamespace.WebServices.Messaging.ProcessScheduledMessages() 'Web Service Call
My questions :
1) Is it going to work ? I mean even if no users are active , will the Thread inside the HttpModule keep running , prevending the application to Shutdown ?
2) Is there any better method to achive what i want ?
Dec 01, 2007 02:11 PM|foreachbiscuit|LINK
Using a HttpModule for this is, I suggest, a bad idea.
You should be aware that there are multiple instances of a particular HttpModule inside an AppDomain...
The HttpRuntime keeps a pool of HttpApplication objects available for a web application so that when requests come in they can be served that much faster. The busier the site, the more HttpApplication objects will be present in the pool.
Each HttpApplication object has a HttpModules collection.. so in your example there will be multiple instances of CSBkGroundWorker and therefore many threads being spawned in the background.
If you want to go down the HttpModule route, you would need to introduce a singleton/lock pattern so that only one thread is ever spawned.. and this is probably more complex than what you had in mind.
Dec 01, 2007 04:18 PM|flougr|LINK
The main reason i tried to do it using HttpModules was that i wanted to keep the background job running even if the application Shutdown (all the user sessions expire).
Although , i am still not quite sure about it.
Is there any better way to implement what i want ? Maybe a singleton Class that i call from a thread in Global.asax ?
Dec 11, 2007 02:58 PM|joshelster|LINK
I think that perhaps the functionality you are trying to achieve here is beyond the scope of what ASP.NET should be used for; HTTP is a stateless, sessionless protocol, so implementing a persistent timer in a web app seems to me like trying to use sandals
as swimming fins.
IMHO, your best bet is to develop a windows service that performs your necessary actions in the background. It will help maintain separation between your web app and your database a little better, and gives you the functionality you're looking for.