I have a base Controller which all the controllers extend, and at base controller I do some session handling. Such as storing user id etc.
Moreover, I have a User class which has other classes such as :
class User
{
public int Id {set;get;}
public virtual Location Location {set;get;}
}
When i populate this from Database (i m using code first EF), obviously Location wont load, Should i store this in the session the way it s?
Main question is I am storing Id in base controller and losing the session everytime i restart the application. why is that happening? i extended session timeout and didnt help.
What are the best practices for session handling? is there a wrapper i can use?
Re: storing User in session: You can, just make sure you don’t ever access the ‘Location’ property without re-attaching it to a session. If you don’t need the data, an option is to just copy the data you do need into a simpler object that doesn’t have ‘Location’ and store that in your Session.
Re: losing the session context, that’s because the default is to store session data in memory, which is lost whenever the application restarts. Take a look at using the StateServer mode for session if you want that state to persist across application restarts (though if you want to persist server restarts or work in a multi-server farm, you’ll want to go the SQLServer route).