1

I've an MVC 5 - based web project and i using HttpSessionStateWrapper class to work with session state. Basicaly i store some user-specific data in session state. Here is my code (without context):

public class Util
{
        private HttpSessionStateWrapper _session;

        public Util()
        {
            _session = new HttpSessionStateWrapper(HttpContext.Current.Session);
        }

        public T Get<T>(string key) where T : class
        {
            var sessionedObject = (T)_session[key];
            if (sessionedObject != null)
            {
                Logger.Instance.LogDebug("Object returned from session. Session Timeout=" + _session.Timeout.ToString() + " Session Id=" + _session.SessionID);
                return sessionedObject;
            }
            return null;
        }
}

The instance of Util class is created one time and serves as a static property for another singleton type. And i've some logging here. So, timeout of session state set to 1 minute via Web.config. But it seems session never epxires for me even within 10 minutes.

Moreover, i noted than on each request SessionId is new, but when getting data from session, the id is equal to first-time generated session id. Session End event also never fired for me, but Session Start does. Here is some logs:

2013-11-11 04:27:38.2578 App Starting 

2013-11-11 04:27:38.6641 Session onStart. Id=0x545pyzbt4e1vzh1h1rr5d5 
2013-11-11 04:27:38.6641 Object returned from session. Session Timeout=1 Session Id=0x545pyzbt4e1vzh1h1rr5d5 

2013-11-11 04:28:06.5263 Session onStart. Id=ujuxasiz5hvzbv15gbvuuxt3  
2013-11-11 04:28:06.5263 Object returned from session. Session Timeout=1 Session Id=0x545pyzbt4e1vzh1h1rr5d5 

2013-11-11 04:29:00.3432 Session onStart. Id=jgwmmh1ubokxn3kfadevfnph 
2013-11-11 04:29:00.3432 Object returned from session. Session Timeout=1 Session Id=0x545pyzbt4e1vzh1h1rr5d5 

2013-11-11 04:39:25.7919 Session onStart. Id=0mocujzp4tbwwvgnkx1mn0qi 
2013-11-11 04:39:25.7919 Object returned from session. Session Timeout=1 Session Id=0x545pyzbt4e1vzh1h1rr5d5 

EDIT: Thanks for replies guys! I decide to refuse using session state at all due to have no time to deep debuging this stuff, and use HttpContext.Current.Items collection, it will serve my needs

3 Answers 3

1

I think you get a new session on each request because you're not storing anything in the session.

Try adding the following to Global.asax.cs:

protected void Session_Start() 
{ 
    // Since .NET 2.0 ASP.NET will create a new session for each request 
    // unless some data is stored in the session, so here we go... 
    Session["dummy"] = 0; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Already tried it, the result is still same - new session id per each request. That is not main problem, i wonder why "first" session never expires. Since Util class is a part of singleton, it initated only one time per App life cycle, That is why it looks in "first" session state all times, but why that session never expires
1

Try leaving the application inactive for a few minutes (running in debug mode) and add a break point to:

protected void Session_Start() {}

in the Global.asax file, and see if this is hit when you return to the application. This should confirm that the session is actually being terminated.

You could also log the Session ID, and log this using Tracing to see when the ID changes.

Comments

1

If there are some serious problems in your app or you have a lot of memory leaks, your IIS app poll will restart the application and you will have exaclty this behaviour. See your system event log. IIs log.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.