views:

238

answers:

1

I have a Silverlight application hosted in an ASP.NET page. I need to do some processing when the application first starts up and start up some background processes (various periodic checks).

I thought that the Global.asax Application_Start event would be a good place to do this, but I find that the Application_Start fires multiple times which I didn't expect. From what I've read it seems that when the last user logs out of my application their session disappears and IIS unloads my application. When it's next requested it gets loaded again and the Application_Start runs again, which is not really what I want.

Is this the expected behaviour? Is there any way to keep the application loaded and not have it restart like this?

Secondly, I have these periodic background processes that I want to run. Maybe a Windows Service would be a better place for them, but having a timer run from within a static class in my application is convenient. Is there a way I can keep these running even though there are no active users?

+1  A: 

I think you are trying to achieve a behaviour which just doesn't fit the web server model well. Many CMSs try to perform periodic tasks etc. by having some user web requests initiate the work, but I have never seen it done with much success.

If you aren't restricted by deployment issues, access rights etc., I would recommend going with the Windows Service approach. Just make sure to incorporate it in your build/deployment process so that that won't become a hazzle.

Rune
I am pretty restricted in my deployment unfortunately. It needs to be as simple as possible with people who are almost end-users deploying new versions of the application. Getting them to also install a Windows Service is probably a bridge too far!
Craig Shearer
Hmmm, okay, that does sound like a problem :-). Will the users just install the Silverlight application (as an out-of-browser Silverlight application) or will they actually be installing your web application. If the latter is the case, how will they do that?
Rune
No, this is on the server only. This is the server part of the Silverlight application - I was just commenting on the general ability of the server administrators.
Craig Shearer
I don't think we are going to find a good solution to this. Even IIS is often configured to recycle the App Pool once in a while.How about making the processes running on Application_Start resilient to being run multiple times? Would that be a feasible solution?How often are your users going to have to install new versions of the application? Creating a setup.exe which installs a Windows Service isn't too hard. Afterwards, the service can be removed using the Add / Remove Programs window.
Rune
Hi Rune - thanks for your help. Actually it isn't a big deal with the process being run multiple times - though I was just a bit surprised that it was happening. I think I'll just try to make my process as lightweight as possible so that it won't be a problem when it gets run again.
Craig Shearer
You're welcome. It's an interesting problem. Good luck with it.
Rune