3

How can I force the Session_Start method on my global.asax to be called every time my application runs in development mode?

I have some code that I need to debug but sometimes it is called, sometimes it isn't. I already tried closing the "ASP.NET Development Server" but no luck.

Bryan
  • 2,870
  • 24
  • 39
  • 44
Diego
  • 34,802
  • 21
  • 91
  • 134
  • Not perfect but useful: entering web.config, making a no-effect change (put some blank outside of tags) and saving it. explanation in https://stackoverflow.com/a/41240646/1830909 – QMaster Nov 24 '17 at 18:02

3 Answers3

4

Check out this article http://sandblogaspnet.blogspot.com/2008/03/methods-in-globalasax.html

Session start event is fired only when a new session for a user starts. Once “Session_Start” for a user is fired then if the user makes subsequent request to any resource within the application this event is not at all triggered. The event is triggered only when the user’s session expires and then the user tries to access any resource in the application again.

Are you sure you aren't looking for Application_Start? If not then as long as you reset the session that event should be called.

EDIT: Also check out this article on someone who was trying to do the same thing as you http://forums.asp.net/t/1608241.aspx/1

From that page:

You are right I just tested it by putting <sessionState timeout="1"/> in my Web.config file. Then I waited 1 minute and clicked on a link on the page. It immediately went back into Session_Start and re-authenticated the user.

EDIT 2: Try calling Session.Abandon() from your start page. It should remove the session and require the user to start a new one.

Gage
  • 7,365
  • 9
  • 47
  • 77
  • 1
    I'd imagine the OP is trying to test `Session_Start` logic within an application and wants to force it to run every time a debugging session begins. – Yuck Feb 21 '12 at 15:40
  • Hi! Yes, the code in on Session_Start and I don't want to remove it from there before I find the issue. So, changing my question, how can I simulate a new session or expire the current one? – Diego Feb 21 '12 at 15:42
  • The edit shows how to do that. Set the in the web.config then just wait a minute and refresh and it should fire. – Gage Feb 21 '12 at 15:42
  • problem is that's a website not a web application, so it doesn't have the "properties page" to configure and on the web config didn't work as well – Diego Feb 21 '12 at 16:07
2

I manage to do it by restarting the "ASP.NET State Service" which is the service used to manage session state on a computer.

Not the ideal solution but solves my problem.

Diego
  • 34,802
  • 21
  • 91
  • 134
  • 1
    simplest way to do this is to run iisreset from the command/search box in the start menu (may need to be run as administrator - im not sure) – dice Feb 21 '12 at 16:27
  • I don't think so, I'm debugging the application on my local computer, so I don't think it uses IIS, it uses the ASP.NET Development Server – Diego Feb 21 '12 at 16:33
0

It can be a pain in the butt, but a combination of restarting the ASP.net Development Server and closing and reopening your browser should reset Session.

Anthony Shaw
  • 8,146
  • 4
  • 44
  • 62