I suppose that application state and session state are two different things.
If I can set expiration time to session state, can I do same to an application state in my web.config ?
I just want to prevent my application from rebuilding after 5 minutes.
I don’t know exactly what happens. When accessing the website for the first time, the site takes 10 seconds to appear. If I return to the site before 5 minutes, the site appears immediately. If there are more than 5 minutes of inactivity, when I access the site again, the site takes 10 seconds. I guess it was the application state Who expired ?
The easiest way I know of to keep your site alive is to make sure traffic is always coming through. If you have access to a scheduled job runner, have it ping a dummy aspx page every minute or two – this way, your app should run indefinitely.
If not, there are techniques to cheat the system. You can have a cache item built, and upon expiration of that cache item, create another cache item. This will perpetually keep running code every X minutes, which will keep the app alive. The problem with this is that if the app ever does die (server reboot, IIS restart, etc.), the app will be dead until someone makes a real request, at which point it will stay alive indefinitely again.
This technique is sometimes used to simulate a task scheduler in ASP.NET. Just keep in mind that it’s not perfect.
EDIT: For clarification, you wouldn’t just create a new cache item, you’d actually make a request to a page which would then in turn create the cache item. This ensures that another request is made through IIS, which in turn runs the full lifecycle, keeping the app alive.
Even if you do set your session timeout high, there’s no guarantee that it will hold. I’ve dealt with hosting companies who will recycle your application pool after just a couple minutes of inactivity, killing any sessions you may have open. They don’t care that you may have sessions open – they are just trying to squeeze every CPU cycle they can out of their servers. So if you’re on shared hosting, you may want to keep that in mind – you may not be able to control how often they kill your app.