title: How to Fire Global.asax's Session_Start Event From Custom HTTP Session State Module?
body: <p>I wrote a HTTP Session State Module that handles my custom Session State Provider.<br/>
  And you know <code>Session_Start</code> and <code>Session_End</code> will work in <code>InProc</code> mode and not in Custom mode.<br/>
  So I want Global.asax to handle a <code>Session_Start</code> method (with my Custom session module and provider) to raise it to some initialisations on my application.<br/></p>
  
  <p>I found <a href="http://www.codeproject.com/Articles/21156/ASP-NET-HttpModule-for-handling-session-end-with-S" rel="nofollow">this Article</a> to handle <code>Sessoin_End</code> Event in StateServer mode, but what about handling the Start Event?!<br/></p>
  
  <p>My Module's Start Event:</p>
  
  <pre><code>public sealed class MyCustomSessionStateModule : IHttpModule
  {
      ...
  
      /*
       * Add a Session_OnStart event handler.
       */
      public static event EventHandler Start
      {
          add
          {
              _sessionStartEventHandler += value;
          }
          remove
          {
              _sessionStartEventHandler -= value;
          }
      }
  
      ...
  }
  </code></pre>
  
  <p>My Web.config Module Configurations:</p>
  
  <pre><code>&lt;httpModules&gt;
      &lt;remove name="Session"/&gt;
      &lt;add name="MyCustomSessionStateModule" type="CustomSessionStateServer.MyCustomSessionStateModule" /&gt;
  &lt;/httpModules&gt;
  </code></pre>
  
  <p>My Web.config Provider Configurations:</p>
  
  <pre><code>&lt;sessionState  mode="Custom"  customProvider="CustomSessionStateStoreProvider" timeout="20"&gt;
      &lt;providers&gt;
          &lt;add name="CustomSessionStateStoreProvider" type="CustomSessionStateServer.CustomSessionStateStoreProvider" /&gt;
      &lt;/providers&gt;
  &lt;/sessionState&gt;
  </code></pre>
  
  <p>I wrote my module in a structure that the session_start will fire in AcquireRequestState's Begin Time.</p>
  
  <pre><code>/*
   * IHttpModule Member
   */
  public void Init(HttpApplication app)
  {
       ...
  
      // Handling OnAcquireRequestState Asynchronously
      app.AddOnAcquireRequestStateAsync(
          new BeginEventHandler(this.app_BeginAcquireState),
          new EndEventHandler(this.app_EndAcquireState));
  
       ...
  }
  
  private IAsyncResult app_BeginAcquireState(object source, EventArgs e, AsyncCallback cb, object extraData)
  {
      ...
      if (_rqIsNewSession)
      {
          // Firing Sessoin_Start Event
          _sessionStartEventHandler(this, EventArgs.Empty);
      }
  }
  </code></pre>
  
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/ms178473(v=vs.85).aspx" rel="nofollow">ASP.net Application Life Cycle</a> The Session State must be available on <code>AcquireRequestState</code> event. but still i have null Session object in Gloabal.asax's Session_Start.<br/>
  I wrote this module according to the <a href="http://referencesource.microsoft.com/#System.Web/xsp/system/Web/State/SessionStateModule.cs" rel="nofollow">Microsoft's .NET 4.5 framework SessionStateModule Source Code</a>.</p>