Z
zino
in an asp.net 2.0 application, I create a dataset, cache it, and then when it
comes to retreive it, I test the cache first as :
" If HttpRuntime.Cache(itemKey) Is Nothing Then ... .. "
and if it's nothing then create the dataset and cache it ... .. .
the function works fine most of the time, but sporadically, once every while
it throws
"System.NullReferenceException: Object reference not set to an instance of
an object"
here is my code:
Public function GetAllClients(serviceCenter as string) as dataset
dim myDelegate as new CacheFactory.myDelegate(AddressOf
ClientDAL.GetAllClients)
dim myCache As Cache = CacheFactory.GetCachedItem("clientCode",
myDelegate)
dim ds As DataSet = ctype(myCache("clientCode"), DataSet) ' SOMETIME
THROWS System.NullReferenceException ERROR, because the cache was not
returned correctly from function 'GetCachedItem'
.... . ...
End Function
class CacheFactory
delegate function myDelegate() As dataSet
Public shared function GetCachedItem(itemKey as string, funct As
myDelegate) As Cache
If httpRuntime.Cache(itemKey) Is Nothing Then
dim ds As DataSet = funct.Invoke()
HttpRuntime.Cache.Insert(itemKey, ds, Nothing)
End If
Return HttpRuntime.Cache
End Function
end class
class ClientDAL
public shared function GetAllClients() As dataSet
' .... ... populate a dataset from database ...... .. ..
end function
end class
the application is under heavy load, and 99% of the time works fine, and
when it start throwing exception it last for ~2 minutes and then everything
goes back to normal.
I don't know if the issue is in the way I'm testing the cache existence or
because the "Shared" type of function "GetCachedItem" ?
comes to retreive it, I test the cache first as :
" If HttpRuntime.Cache(itemKey) Is Nothing Then ... .. "
and if it's nothing then create the dataset and cache it ... .. .
the function works fine most of the time, but sporadically, once every while
it throws
"System.NullReferenceException: Object reference not set to an instance of
an object"
here is my code:
Public function GetAllClients(serviceCenter as string) as dataset
dim myDelegate as new CacheFactory.myDelegate(AddressOf
ClientDAL.GetAllClients)
dim myCache As Cache = CacheFactory.GetCachedItem("clientCode",
myDelegate)
dim ds As DataSet = ctype(myCache("clientCode"), DataSet) ' SOMETIME
THROWS System.NullReferenceException ERROR, because the cache was not
returned correctly from function 'GetCachedItem'
.... . ...
End Function
class CacheFactory
delegate function myDelegate() As dataSet
Public shared function GetCachedItem(itemKey as string, funct As
myDelegate) As Cache
If httpRuntime.Cache(itemKey) Is Nothing Then
dim ds As DataSet = funct.Invoke()
HttpRuntime.Cache.Insert(itemKey, ds, Nothing)
End If
Return HttpRuntime.Cache
End Function
end class
class ClientDAL
public shared function GetAllClients() As dataSet
' .... ... populate a dataset from database ...... .. ..
end function
end class
the application is under heavy load, and 99% of the time works fine, and
when it start throwing exception it last for ~2 minutes and then everything
goes back to normal.
I don't know if the issue is in the way I'm testing the cache existence or
because the "Shared" type of function "GetCachedItem" ?