Reputation: 83264
When is the Session
property in Controller
Initialized?
From what I see it is not initialized when the constructor is called. So when it's initialized?
Upvotes: 1
Views: 1146
Reputation: 655
So this is old, but I thought I'd leave the answer here for someone in the future.
http://forums.asp.net/t/1500473.aspx
Some guy says the Session is created when you first use it. My own experience bears this out, with one caveat, if you have two action methods executing simultaneously then the first one to access the Session appears to end up with a different Session to the second. I put a lock around my code to try and solve this, but ended up solving it by initialising my Session earlier on in my process.
Upvotes: 1
Reputation: 532575
It looks to me from the source, that the controller context (which contains the HttpContext and thus the Session) is provided to the controller when the action is invoked by the ControllerActionInvoker. It's also available to filters via the filter's context object on the filter's main methods.
Upvotes: 3