재우니의 블로그


원문 : Mystries of when does Session_End event fires in Asp.Net


While thinking of Session_End event one of the most important things is that the event is supported only when the session is maintained Inproc. The session_End event will not fire if you are using State server or SQL server.


Session_End 의 이벤트에서 가장 중요한 것중 하나로 생각되는 것은 session 이 Inproc 으로 유지될때 오직 이 이벤트가 제공된다는 점이다. 만약에 State server 나 Sql Server 를 사용할 대는 발생되지 않는다.

Another important point to note is that Session_End event is fired by a worker process. This also means that Session_End event might not have all the permission that a normal page request has. So if you are trying to connect to the SQL server with Windows account, Session_End event might not have required permission, although you can connect to the SQL in Session_Start event.

또 주목해야 할 중요한 것은 Session_End 이벤트가 worker process 에 의해 발생된다는 점이다. 무엇을 의미하느냐면 Session_End 이벤트는 보통 page 요청을 할 때 모든 permission 을 갖고 있지 않는다. 그래서 Window 계정을 가지고 Sql Server 에 연결을 시도할 경우,Session_End 이벤트는 permission 을 요청되지 않을것이다. 비록 Session_Start 이벤트에서 SQL 을 연결할 수 있을 지라도 말이다.


Now when does the Session_End event fires?

Session_End will fire after a given amount of time where session has been inactive. The given time can be changed globally in the web.config file with the time out attribute or for individual session with the help of Session.Timeout property.

Session_End will also fire when someone calls the Session.Abondon method. Note that the Session_End event will not fire immediately. As said before the Session_End event is fired from worker process. Hence the session ID will also be same.


그러면 Session_End 이벤트가 언제 발생되는 걸까? Session_End 는 session 이 비활성화되는 곳에서 많은 시간이 주어진 후 발생될 것이다. 주어진 시간은 Session.Timeout 속성의 도움으로 개인적인 session 을 위한것이거나 web.config 의  time out 어트리뷰트에서 전역적으로 변경될 수 있다.
Session_End 는 Session.Abondon 메소드를 호출할 때도 발생된다. 주목할 것은 Session_End 이벤트가 즉시 발생되지 않는다는 점이다. 이전에 말씀드렸지만, Session_End 는 work process 에 의해 발생되어 진다는 점이다. 어쨋든 session ID 또한 동일하게 될것 이다.

Remember the Session_End event will not fire in case the user closes the browser. HTTP is a stateless protocol. There is no way for the server to understand that the browser has been closed.

Also another very important thing to note here is that if you do not save anything in the session the Session_End event will not fire. There must be something saved in the session atleast once for the Session_End event to fire.

Session_End 이벤트는 사용자가 브라우저를 닫았을 경우 발생되지 않는다. HTTP 는 비연결형 프로토콜이다. 브라우저가 닫혀졌다는 것을 서버에서 알 수 있는 방법이 없다. 
또한 다른 중요한 것 중 하나는 만약에 session 에 어떤 것도 저장하지 않으면 Session_End 이벤트는 발생하지 않는다. Session_End 이벤트를 한번이라도 발생하기를 원한다면, 적어도 세션에 어떤 값이라도 저장되어 있어야 한다.


This also means that if you save something in the session in the first request and abandon the session in the same request the Sesison_End event will not fire, as there was nothing saved in the session ever.


또한 이는 만약에 처음 request 에서 세션에 무엇인가를 저장하고, 동일한 request 에 세션을 abondon 을 하면 session 에서 저장된 어떤것도 저장되어 있지 않아서 Session_End 이벤트는 발생되지 않는다.