Dashboard > ActiveRecord > ... > How to > Enable Session per Request
Enable Session per Request
Added by Daniel Rothmaler, last edited by Damon Wilder Carr on May 10, 2008  (view change) show comment
Labels: 
(None)


If you're using lazy collections you need a session that lives through the lifetime of the web request. This is easily accomplished.

Please ensure that you've followed the steps described in Configuring for Web applications

Sample global.asax:

<%@ Application Inherits="Company.Project.Web.GlobalApplication"  %>

Sample Web Application class:

public class GlobalApplication : HttpApplication
{
  ...

  public GlobalApplication()
  {
    this.BeginRequest += new EventHandler(OnBeginRequest);
    this.EndRequest += new EventHandler(OnEndRequest);
  }

  public void OnBeginRequest(object sender, EventArgs e)
  {
    HttpContext.Current.Items.Add( "nh.sessionscope", new SessionScope() );
  }

  public void OnEndRequest(object sender, EventArgs e)
  {
    try
    {
      SessionScope scope = HttpContext.Current.Items["nh.sessionscope"] as SessionScope;
      if (scope != null)
      {
        scope.Dispose();
      }
    }
    catch(Exception ex)
    {
      HttpContext.Current.Trace.Warn( "Error", "EndRequest: " + ex.Message, ex );
    }
  }
}

That's it. You don't have to change anything else.

Here are some areas that typically come up when trying to package these concepts and solutions in a personal/corporate framework for reuse:

  • You would like to re-use code across projects, even across ORM Implementations
  • The IoC Container is an asset you can use here as well. The following example illustrates.
  • Although ActiveRecord has an HttpModule you can use, the techniques discussed are marginal improvements. There is nothing wrong with just using that solution however.
  • The ActiveRecordFacility is where these features start to get powerful. It registers ISessionFactory and ISessionFactoryHolder for you in Windsor/MicroKernal, so if you expose the points of injection, you will get these.
  • HOWEVER: It does not register ActiveRecord classes as Components (as far as I could tell) so if you would like either of the IXXFactoryXXX instances injected in these steps are necessary (and can save a lot of time).

NOTE: Often ASP.NET will process HTTP requests for items such as images through the Begin/End page lifecycle. This is fixable via changing the configuration, however this technique injects a safe-guard.

You could also of course use Windsor/MicroKernal to retrieve and release your ISessionScope components by accessing them via a shared container. This code shows both approaches.

For an extensive coverage of these topics and more, check out advanced topics and samples.

See also

  • [Facility:ActiveRecord]

Site running on a free Atlassian Confluence Community License granted to Castle Project. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.4 Build:#809 Jun 12, 2007) - Bug/feature request - Contact Administrators