0
votes

All,

I have an ASP.NET(C#) that functions as expected with the integrated debugger/web server. However when I move it over to a IIS server it appears as though the cache object is not being set. Can anyone offer any help?

Here is the class that sets the cache and subsequent cookie.

   class globals
    {
        public NameValueCollection values;
        private static string m_visitNumber ="";

    public globals()
    {
        string userName = HttpContext.Current.Request.Cookies["PatientDischargeSummary"].Value;
        values = HttpContext.Current.Cache[userName] as NameValueCollection;
    }

    public globals(NameValueCollection form)
    {
        // Copy the form values.
        values = new NameValueCollection();
        values.Add("txtCR", form["txtCR"]);
        values.Add("txtName", form["txtName"]);



        // Add the values to the cache.
        //HttpContext.Current.Cache.Insert(form["txtUserName"], values, null, System.Web.Caching.Cache.NoSlidingExpiration, TimeSpan.FromMinutes(5));
        HttpRuntime.Cache.Insert(form["txtUserName"], values, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
        //HttpContext.Current.Cache.Insert(form["txtUserName"], values, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);

        // Add the username to the cookies.
        HttpCookie cookie = new HttpCookie("PatientDischargeSummary", form["txtUserName"]);
        cookie.Expires = DateTime.Now.AddMinutes(30);
        cookie.HttpOnly = true;
        HttpContext.Current.Response.Cookies.Add(cookie);
    }

An example of me using the cache:

globals pcs;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {

            pcs = new globals();


            lblActiveEditor.Text = pcs.values["txtName"];


        }

    }

Generate the following error under IIS:

[NullReferenceException: Object reference not set to an instance of an object.] navigationtest.Demographics.Page_Load(Object sender, EventArgs e) in Demographics.ascx.cs:23 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +34 System.Web.UI.Control.OnLoad(EventArgs e) +99 System.Web.UI.Control.LoadRecursive() +47 System.Web.UI.Control.LoadRecursive() +131 System.Web.UI.Control.LoadRecursive() +131 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

Any thoughts?

2

2 Answers

1
votes
pcs.values["txtName"]

That's null and it's already gone from Cache when you're trying to receive it. Your code seems to cache some per-request data obtained from users' input and there's nothing to guaranty the availability of that data in your cache.

Every cache access should be prepared to fetch the data from the data source in case of a miss, so, in your case I would use the user's session (although I'm not aware of your architecture, how many servers...) Using the user's session would persist that data for the life of the appdomain or the session itself (whichever ends first), so you should also be prepared to query it again in case of a miss / timeout / appdomain shutdown.

0
votes

Should you be using HttpContext.Cache instead of HttpRuntime.Cache ?