Wednesday, March 28, 2012

Context.Cache not available intermittently using UpdatePanel

I store some settings used by my application in the Context.Cache. When I postback using an UpdatePanel, sometimes it is available and other times it is simply Nothing as if it doesn't exist. There seems to be no consistent pattern and the code has not changed. It just works sometimes and other times it does not.

Is this a known issue with the ASP.NET Ajax team or is there something I am not aware of that needs to be set or configured to support getting the Context.Cache properly?

Thanks for any help or insight.

Regards,

Don

Hi,

The cache returned by the Context object is actually a static object shared by the whole application. It's not likely to be null.

I suspect that the NullReferenceException is thrown by the Context object, please try using HttpRuntime.Cache to access it rather than Context.Cache.


Another possibility is that the cache is expired.Please trying specifying disableExpiration="true" [ http://msdn2.microsoft.com/en-us/library/ms228248.aspx ].

Hi Raymond - Thanks for the reply and the help. Unfortunately, using the HttpRuntime.Cache instead of Context.Cache produces the same intermittent results. I am convinced this is a problem with ASP.NET Ajax v1.0. I have used the Cache functionality for some time on other projects and have never had an issue like this. Also, this is not a timeout error as I am testing immediately after I fire up the application and as I said, sometimes it works and then for no reason on an Ajax partial page postback it will fail and nothing will be there.

My solution for this is to rely on the Session and so far I have had no issues with that.

Thanks again for your help and if you have any other suggestions, or would like me to post my code, let me know. Thanks!

Don


Hi Don,

Please show me a small, self-sufficient sample.

Thanks.


Hi Raymond - Below are the basic steps I go through:

In my Global.asax:

----------------------------------------------------------

Sub Session_Start(ByVal senderAsObject,ByVal eAs EventArgs)
HttpRuntime.Cache.Insert("MCSApplicationSettings", ApplicationSettingsLoader.GetApplicationSettings(ConfigurationManager.AppSettings("MCSApplicationSettingsURL")))

EndSub

----------------------------------------------------------

This gets populated for each session that fires up.

In my ASPX Page:

----------------------------------------------------------

Private applicationSettingsAs Hashtable =Nothing

PrivateSub Page_Init(ByVal senderAsObject,ByVal eAs System.EventArgs)HandlesMe.Init

_applicationSettings = CType(HttpRuntime.Cache.Get("MCSApplicationSettings"), Hashtable)

EndSub

----------------------------------------------------------

Everything works fine until I do a partial postback and try to access the Cache. Sometimes it is there and sometimes it is not.

Thanks,

Don

No comments:

Post a Comment