4

I am a bit confused about the following: I set up an asp.net Website with some logic in the Session_Start() of the Global.asx. I expected that this even will only trigger once during a session. However the event fired with every single request.

When I declare a dummy session object this fixed the problem. Also I can fix this problem when I add <sessionState mode="InProc" /> in the web.config. I use IIS7 and I checked the default value and this is already set to "In Process".

I am missing something? Is this normal behaviour? I was expecting this event to work even if I don't declare a session object.

Charles
  • 50,943
  • 13
  • 104
  • 142
Dirk Dooms
  • 101
  • 1
  • 2
  • 4

4 Answers4

1

Store something in a Session object.

Session["dummy"] = 1;

http://blogs.msdn.com/b/nikhiln/archive/2007/06/21/detecting-session-timeout-in-asp-net-2-0-web-applications.aspx

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
  • This worked for me in a scenario where the `Session_Start` kept firing when using the Redis provider. The SessionID was always correct, but it kept thinking the session was new on each request. – Kevin Giszewski Jul 13 '16 at 17:44
1

I think this happens if the browser being used does not allow cookies. As well if you application pool is being restarted the session will be lost.

<sessionState cookieless="true" />

That will solve issues with cookies not being allowed..

StefanE
  • 7,578
  • 10
  • 48
  • 75
1

You'd check:

  • Has your web browser cookies enabled?
  • If you want browser cookies to be disabled, you'll need cookie-less session state management (this isn't a good option, because session parameter will be in the query string...).
  • During the Application_Start in your Global.asx, if you do something there, have you checked isn't throwing some exception that makes your application crash and end?
  • During any stage after Application_Start, and before Session_Start, are you doing something? If this is your case, check the same thing as previous point.
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

I recently came across this issue and found that if any file is updated inside bin folder it automatically restart application which triggers Application_Start and Session_Start events. In my case i was creating log files inside bin folder and updates in log files restarts application. I simply moved my log files outside of bin folder to resolve issue.

Zeshan
  • 71
  • 6