Reputation: 21
I have a web service in IIS and in it's ctor I'm building an object which I save on the HttpContext.Cache object if it doesn't exists in it yet.
When the IIS is restarting (for any reason) the cache is erased and the object will be created only when a user will activate a function for the first time. I want the Ctor to be executed when the IIS is loaded, without waiting to a user's action.
Upvotes: 2
Views: 160
Reputation: 13410
Thats a common thing with caching data and the web context actually gets recycled because no one is using the site for a certain amount of time (usually 20 minutes).
You can either handle your cache in a way that it handles the cache independent from the current application context (e.g. persistent cache, distributed cache...)
Or you simply modify your IIS to not shut down the application pool after 20minutes idle time. You can find instructions to do this here: http://brad.kingsleyblog.com/IIS7-Application-Pool-Idle-Time-out-Settings/
Regarding your question of how to initialize something on ISS start: That's not really true, the IIS does not get shut down, it is just the application pool which frees resources, it actually shuts down your web application which runs within the application pool. Then, whenever a user hits that web application, the application pool will start that application again. The best way, as already mentioned by someone else, is to initialize any resources within your global.asax. The global.asax app_start will be executed only and exactly in this very moment where your web application gets initialized again...
Upvotes: 0
Reputation: 13367
I'd instantiate the object in the Application_Start
event of the global.asax
. Your object's contructor will fire as soon as any resource in the app is requested. The VERY first user to hit this resource is going to incur a hit. AFAIK, there's no event for when an app pool starts.
Upvotes: 2