0

I have some logic as part of Session_Start method in Global.asax.cs file. the logic loading some information from the database to session variable so all other Controller actions can use them. Everything if working fine until the session expire.

Since i am using Angular (SPA), users actions not goes to the server frequently so the session can just expire after 20 minutes of "server inactivity" (Its not inactivity of the user because he it working on the client side).

Once he want to perform an action that needs to execute a MVC controller action, the controller constructor doesn't find the information on the cache and throws an error.

I expected that the Session_Start will triggered before invoking the controller contractor but the Sesion_Start event not firing at all because i am not assigning information to the session variables, i need only to read them but they are not available.

i tried to assign a dummy data to the session in order to fire the Session_Start event but i can do so only from the controller action but i need the session_start event will be executed BEFORE the Controller constructor.

Is there a way to trigger Session_Start on EVERY server request when the session is not exist (expired)? Is there an option to fire it before the controller constructor?

the problem is that i can not do so from Application_BeginRequest() for example because sessions can not be accessed or use at that scope.

*** This problematic behavior happens only in production. on dev the session_start always fire. i understand its "by design" so it a bit hard to reproduce **

Mark G
  • 1,950
  • 17
  • 25
Omtechguy
  • 2,562
  • 3
  • 28
  • 58
  • 1
    While it doesn't necessarily answer the question, couldn't you abstract out the session data so it's lazily loaded? Your controllers would call a 'helper' that handles pulling the data from the session, if it's not there, get it from the database and store back in the session. – Phil Cooper Jul 18 '18 at 12:42
  • can you maybe show use your global.asax? It test it on my project and it works as expected. – Darem Jul 18 '18 at 12:51
  • @PhilCooper - I thought about that and i found that i can not save data to the session on the controller constructor. Only on the controller action there is an access to the session object and i need to save the data before initializing the controller. – Omtechguy Jul 18 '18 at 13:29
  • @Darem - my global.asax will not help because i am calling to a BL functions from there but you can just do something like this: Session["x"]="y" and you will see that if you will leave the system without touching for 30min and trying to access the session variable from a controller constructor (not an action) the Session_Start will not be invoked. Locally it works but not on remote server. i understand its by design. – Omtechguy Jul 18 '18 at 13:36
  • @Omtechguy ok than I cant help you here, because I cant produce the error on my system. Sorry! – Darem Jul 18 '18 at 13:38
  • @Darem - Maybe you know a way to add information to session before the controller constrictor invoked? something like OnActionExecuting but for controller and not for a method/action? – Omtechguy Jul 18 '18 at 13:41
  • @Omtechguy you can try to overwrite the Initialize-Method on your controller so you can get the http context. https://stackoverflow.com/questions/3432685/accessing-httpcontext-request-in-a-controllers-constructor the third anwser from top. – Darem Jul 18 '18 at 13:50
  • @Omtechguy ok, although now I'm wondering why you need to 'add information to session before the controller constructor invoked' and this question sounds a lot like an [XY problem](https://mywiki.wooledge.org/XyProblem). – Phil Cooper Jul 18 '18 at 14:51

0 Answers0