tags:

views:

33

answers:

1

I have an ASP.NET project which is a front-end to a database. In addition to the large tables, the DB contains a few small tables to help normalize the larger tables with common values. I have a VB.NET project which loads the smaller tables into memory, using "Shared" (i.e., "static" in C#) member variables, and uses them. I have a call to load the tables in Global.asax - Application_Start. This works for a while. That is, Application_Start runs when I first run my project, loads the cached values, and will correctly keep them in memory for a while.

What I'm seeing (when running my project via Visual Studio 2008 Debugger, hosted locally) is:

A) The Application_Start code will run more than once. Not in a row, but after the user has navigated to some other pages, I'll see (my breakpoint in) another call to initialize the cache, coming form Application_Start. Is it expected?

B) The "Shared" variable that was set to True when the cache was initialized is now False again (which should only happen when the class is first loaded). Similarly, all the data that was chached is no longer present. That is, it looks like VB is unloading all the Shared members. Is this expected?

If these are the expected behaviors, is there a way to do what I want? The code is in a module that is also used by other (non-ASP.NET) projects, and seems to work correctly for them. I'd rather not have to duplicate this functionality for something specific to ASP.NET, but would like to know what my options are. Thanks for any advice.

A: 

Here is an article you might find helpful about Caching Data at Application Startup. It sounds like you are doing everything right, but Application_Start should only get called once, unless some external change happens that restarts the app pool, but in this case i would think you would get detached from the debugger (assuming you are attached to the app pool process for your asp.net application).

Matt Dearing
The app pool recycling would also account for your cached data being lost. In addition to checking the event viewer (like Mr. Stratton suggested) you could create an Application Level Event Handler to see if any uncaught exceptions are causing the app pool to crash. Here is a helpful link http://msdn.microsoft.com/en-us/library/fwzzh56s%28VS.80%29.aspx
Matt Dearing
Why cache the data at application startup? Why not cache it when it's first accessed? i.e. check if data i need is in cache, if in cache, return data, if not in cache, get data, add to cache, return data.
FiveTools
In a lot of cases you could lazy load the cached data, but that doesn't change the fact that Application_Start shouldn't be called more than once.
Matt Dearing
Thanks - the link seems official, and indicates it at least SHOULD work as I expected (which answered my question). I guess I'll now have to narrow down why it's restarting when I don't expect it to.
Andy Jacobs