3

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?

Dillie-O
  • 29,277
  • 14
  • 101
  • 140

4 Answers4

4

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
  • 22,595
  • 11
  • 77
  • 101
  • Hmm, didn't know about that one. I'm off to check that out. What's the difference between the two? – Dillie-O Feb 17 '09 at 22:45
  • 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 Feb 18 '09 at 17:03
2

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
  • 1,702
  • 2
  • 18
  • 34
  • Should I remove the callback function from the initial load or the cache expired method? – Dillie-O Feb 17 '09 at 22:42
  • 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 Feb 18 '09 at 00:01
  • 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 Feb 18 '09 at 17:02
0

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

oglester
  • 6,605
  • 8
  • 43
  • 63
  • 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 Feb 17 '09 at 22:41
0

Maybe you call the Method with AJAXPro or something.

Echilon
  • 10,064
  • 33
  • 131
  • 217