I have created a WCF method that runs an infinite loop, polling every 5 minutes. It writes 2 items to cache, the main item I’m retrieving and the LAST_POLLED. This is successfully called on app start of our web site:
//infinite loop
while (true)
{
try
{
_cache.RefreshCache();
WcfCache.SetCache(LAST_POLLED, DateTime.Now);
}
catch(Exception ex)
{
//logging exception to database
}
//essentially polling interval, though dependent on how long it takes to complete task
Thread.Sleep(Polling_DELAY);
}
The LAST_POLLED datetime should have the time it was polled. Occasionally, though, it’s storing Jan 1, 0001. I imagine this must be due to the cache disappearing. The app pool for this WCF site is NOT set to Recycle NOR are the worker processes set to shutdown after being idle. This will work for days at a time, but hasn’t worked a full week yet.
- Why is this cache getting reset?
- Is there a better way to code this? I didn’t want to create a separate windows service for this to keep down the number of projects/apps, but I can.
This blog post, although not specifically addressing your topic, does have some good information about what might cause your app pool to get recycled.
http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx
It also contains some information that may solve your problem (HostingEnvironment.RegisterObject)