locked
Session becomes null even though it is never changed RRS feed

  • Question

  • User-1754253538 posted

    In the project i am developing a file is saved in a Session, after that moment that object is only read, however sometimes (because it does not always happen) that Session becomes null, looking online i have already found a solution that uses databases but this it is a school project and we have not yet arrived at implementing a database. What should i do? Thanks in advance.

    Monday, February 1, 2021 3:16 PM

Answers

  • User571301025 posted

    Hi IGieckl,  Storing large object in Session like Files in not a recommended way to use. So try to use Database. Since you don't want to go with DB also in that case read the file whenever you want. It will be better choice. 

    Hope this helps!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2021 7:52 PM
  • User-821857111 posted

    Sessions are volatile and can be lost for a number of reasons including memory pressure on the web server, app pools recycling and applications restarting. You should always check whether a session item for null before you try to access it. If it is null, load the item into session again.

    if(Session["file"] == null)
    {
        Session["file"] = File.ReadAllText("/somefile");
    }
    
    var fileContent = Session["file"];



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 2, 2021 7:00 AM

All replies

  • User571301025 posted

    Hi IGieckl,  Storing large object in Session like Files in not a recommended way to use. So try to use Database. Since you don't want to go with DB also in that case read the file whenever you want. It will be better choice. 

    Hope this helps!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2021 7:52 PM
  • User-821857111 posted

    Sessions are volatile and can be lost for a number of reasons including memory pressure on the web server, app pools recycling and applications restarting. You should always check whether a session item for null before you try to access it. If it is null, load the item into session again.

    if(Session["file"] == null)
    {
        Session["file"] = File.ReadAllText("/somefile");
    }
    
    var fileContent = Session["file"];



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 2, 2021 7:00 AM