views:

392

answers:

4

I have a bunch of simple lookup tables cached in my asp.net application since the source data is on a seperate server from our main web architecture and it changes infrequently. I've been following answers here and various documentation and I have my initial load function call the following:

HttpContext.Current.Cache.Insert("CheckLocations", GetAllCheckLocations(), _
                                 Nothing, DateAdd(DateInterval.Day, 1, Now()), _
                                 System.Web.Caching.Cache.NoSlidingExpiration, _
                                 CacheItemPriority.Normal, _
                                 New CacheItemRemovedCallback(AddressOf CheckLocationsExpired))

For my cache expired callback, I have the following code.

Public Shared Sub CheckLocationsExpired(ByVal key As String, ByVal value As Object, ByVal reason As CacheItemRemovedReason)

   Dim dtCheckLocation As New ReferenceSchema.CheckLocationDataTable
   dtCheckLocation = GetAllCheckLocations()

   HttpContext.Current.Cache.Insert("CheckLocations", dtCheckLocation, Nothing, _
                                    DateAdd(DateInterval.Day, 1, Now()), _
                                    System.Web.Caching.Cache.NoSlidingExpiration, _
                                    CacheItemPriority.Normal, _
                                    New CacheItemRemovedCallback(AddressOf CheckLocationsExpired))

End Sub

For the record, the GetAllCheckLocations method simply calls a web service and parses the results into the data table being stored.

Now when I recompile the application for local testing, everything still functions fine, but I find the following exception message in my log file:

System.NullReferenceException: Object reference not set to an instance of an object. at EAF.CacheMethods.CheckLocationsExpired(String key, Object value, CacheItemRemovedReason reason) in C:\Projects\HR\EAF 2.0\DAL\CacheMethods.vb:line 434 at System.Web.Caching.CacheEntry.CallCacheItemRemovedCallback(CacheItemRemovedCallback callback, CacheItemRemovedReason reason)

I verify that the data is indeed there and up to date, and nothing in the command arguments seems out of place when I step through the debugger.

Does anybody know what I'm missing here? Is this another one of those "nuances" like the Reponse.Redirect issue where terminating the processing technically throws a thread abort exception?

A: 

My initial thought is that GetAllCheckLocations is throwing the exception or returning null.

Greg Ogle
GetAllCheckLocations is returning data, but I'll try to tap into it directly in the cache expiry. There could be something with security (since we're using impersonation) that could hang that up.
Dillie-O
A: 

Does it still exception out when you don't give it a callback function? Seems more like the delegated function is having issues with null objects.

dnord
Should I remove the callback function from the initial load or the cache expired method?
Dillie-O
Hey!!! This looks to have promise! I didn't see any errors, and I'm going to try it with a couple of other cached items that were having similar issues and see if the data still remains.
Dillie-O
This did the trick. Turns out I was doing some self referencing, which isn't what all the further examples I've been digging up are doing.
Dillie-O
+2  A: 

You may want to use HttpRuntime.Cache instead. It's possible that HttpContext.Current is null if you are calling it from a unit test or such.

slf
Hmm, didn't know about that one. I'm off to check that out. What's the difference between the two?
Dillie-O
While it was the callback function causing the issue, I did notice changing to the HttpRuntime also made things run smoother, so this deserves a +1.
Dillie-O
A: 

Maybe you call the Method with "AJAXPro" or something...