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:
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]
