locked
Session not initialized RRS feed

  • Question

  • User464014006 posted

    Hi,

    I got a session variable delcared as below, It is always not equal to NULL even though I call it for the very first time after I reset my IIS, even restart my visual studio and rebuild the whole project. Any idea on this?

    protected Foo mySessionVariable
    {
         get
         {
              if (Session["mySessionVariable"] == null)
              {
                  Foo myBizObj = new Foo ();
                  Session["mySessionVariable"] = myBizObj;
              }
              return Session["mySessionVariable"] as Foo;
         }
    }
    
    Thursday, June 2, 2011 5:09 AM

All replies

  • User794480159 posted

    Search for Session["mySessionVariable"] in your whole project and make sure its value is not being initialized somewhere else in the project. If you have reset the IIS, then the session should be null for the first time.

    Note: If you are running your site from Visual studio, then locate the asp.net development server icon in the system tray and stop it Or alternatively go to task manager and kill the webdev.webserver.exe process.

    Thursday, June 2, 2011 5:46 AM
  • User464014006 posted

    Searched the entire  solution, the only Session["mySessionVariable"] I can find is the one I declare, reset IIS and tried again, same result again.

    Thursday, June 2, 2011 6:17 AM
  • User1315112726 posted

    Please make sure you have enabled session in web.config.

    under system.web, sessionState mode property to InProc.

    Also, make sure it enabled in page level also.

    Thursday, June 2, 2011 6:37 AM
  • User794480159 posted

    I am not sure what could be the problem.

    The only thing i can see from your code is mySessionVariable property that would never be null even for the first time.

    But Session["mySessionVariable"] ideally should be null if you restarted your application.

    Note: Sorry if i misunderstood your question, are you talking about mySessionVariable property or value of Session["mySessionVariable"]. If your question was about mySessionVariable not being null for the first time, then it won't be according to your code. Because you are initilizing Session["mySessionVariable"] if it is null and then returning the value.

    Thursday, June 2, 2011 6:38 AM
  • User464014006 posted

    is there any different between its "properties" and "value"?

    From my code i think you can see that I'm trying to check whether it is null for the Session["mySessionVariable"], not the properties. From the immediate window, I notice that Session["mySessionVariable"] actually have some values that supposed to be retrieved from database if it is null.

    Thursday, June 2, 2011 6:46 AM
  • User-1359474226 posted

    There is nothing wrong in your code. Is this happening for only this session variable or all the session variables.

    Where are you storing the session, is it inproc or out of process, like in SQL server

    Hope this helps

    Thursday, June 2, 2011 9:04 AM
  • User464014006 posted

    There is nothing wrong in your code. Is this happening for only this session variable or all the session variables.

    Where are you storing the session, is it inproc or out of process, like in SQL server

    Hope this helps

    Only happen to this session variable, it's inproc

    Thursday, June 2, 2011 9:44 PM
  • User-1359474226 posted

    I don't see any issue with the code. Can you provide some more information. Is there any exception been thrown in the Foo constructor.

    Hope this helps

    Friday, June 3, 2011 2:18 AM
  • User464014006 posted

    There's no exception thrown, out of the blue it works now, but I still don't know what is the reason it make it behave that way in previously

    Friday, June 3, 2011 2:22 AM
  • User-1359474226 posted

    There could be a situation where the session timeout happened. Your application pool got recycled.

    Good to hear that your issue got resolved.

     

    Friday, June 3, 2011 2:35 AM