title: Session Start and Session End in Umbraco
body: <p>I am using umbraco 7.0 and I need to add some custom code on application start, session start and session end. As for registering events in umbraco we have to inherit from <code>Umbraco.Core.ApplicationEventHandler</code>, I have sone so. But in that we can only override <code>ApplicationStarted</code> and not Session related as we can do in Global.asax.</p>
  
  <p>I have seen <a href="https://stackoverflow.com/questions/15872770/global-asax-in-umbraco-6">Global.asax in Umbraco 6</a> but I can not access Session as shown in that answer <code>if (Session != null &amp;&amp; Session.IsNewSession)</code>, maybe it was umbraco 6 and something changed in umbraco 7.</p>
  
  <p>Any solutions?</p>
  
  <p>Heres the code suggested in mentioned post.</p>
  
  <pre><code>public class Global : Umbraco.Web.UmbracoApplication
  {
    public void Init(HttpApplication application)
    {
      application.PreRequestHandlerExecute += new EventHandler(application_PreRequestHandlerExecute);
      application.EndRequest += (new EventHandler(this.Application_EndRequest));
      //application.Error += new EventHandler(Application_Error); // Overriding this below
    }
  
    protected override void OnApplicationStarted(object sender, EventArgs e)
    {
      base.OnApplicationStarted(sender, e);
      // Your code here
    }
  
    private void application_PreRequestHandlerExecute(object sender, EventArgs e)
    {
      try
      {
        if (Session != null &amp;&amp; Session.IsNewSession) // Not working for me
        {
          // Your code here
        }
      }
      catch(Exception ex) { }
    }
  
    private void Application_BeginRequest(object sender, EventArgs e)
    {
      try { UmbracoFunctions.RenderCustomTree(typeof(CustomTree_Manage), "manage"); }
      catch { }
    }
  
    private void Application_EndRequest(object sender, EventArgs e)
    {
      // Your code here
    }
  
    protected new void Application_Error(object sender, EventArgs e)
    {
      // Your error handling here
    }
  }
  </code></pre>