DLeh
DLeh

Reputation: 24395

Will ASP.NET application pool recycling refresh a cache on a static object?

I have an application that uses a cache for certain objects, and that cache is implemented as a static Dictionary. I'm looking at using some of this code for a new ASP.NET project. Since the cache will be stored on the server, instead of built every time the application is run, I will need to refresh the cache from time to time.

My question is this: will application pool recycling ever rerun the static constructor of my class which sets up the cache?

I don't know much about how application pools work, but from my understanding, they"shut down" when the site isn't accessed for a long time, and restart when it is accessed again. During this restart, would static objects be "instantiated" again, or are those still preserved in memory through the "shut down" process?

Upvotes: 3

Views: 1075

Answers (1)

haim770
haim770

Reputation: 49135

Yes. Static constructors would re-run.

Yet, it's still wiser to initialize data on Application_Start event rather than relying on static constructors because it behaves in more predictable way.

Upvotes: 2

Related Questions