2017-02-14 2 views
2

Session_Start() 및 Session_End()를 사용하여 세션 상태 이벤트를 catch해야합니다. 내 프로젝트의 특성상 소스 코드를 변경하지 못하도록하므로이 메소드를 global.asax 파일에 추가 할 수 없습니다. 어떻게하면 좋을까요? 이미 global.asax.cs 클래스를 상속 받았다가 거기에 메소드를 추가했지만 충돌하지는 않습니다.global.asax (C#, .Net) 외부의 세션 상태 이벤트 잡기?

답변

0

HTTP Module으로이 작업을 수행 할 수 있습니다. 모듈이 확실로드되어 있는지 확인하여 web.config에서 지금

public class SessionCatchingModule : IHttpModule //You will need to import System.Web 
{ 
    public void Init(HttpApplication context) 
    { 
     //Get the SessionstateModule and attach our own events to it 
     var module = context.Modules["Session"] as SessionStateModule; 
     if (module != null) 
     { 
      module.Start += this.Session_Start; 
      module.End += this.Session_end; 
     } 
    } 

    private void Session_Start(object sender, EventArgs args) 
    { 
     //Oh look, a session has started 
    } 

    private void Session_End(object sender, EventArgs args) 
    { 
     //Oh look, a session has ended 
    } 

} 

:

<system.webServer> 
    <modules> 
    <add name="SessionCatchingModule" 
     type="YourNamespace.Goes.Here., SessionCatchingModule" /> 
    </modules> 
</system.webServer> 
다음은 예입니다