Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Linq;
using System.Web;
using NHibernate;
using System.Collections.Generic;

// This is needed for the DependencyResolver...wish they would've just used Common Service Locator!
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dependencies;
using System.Web.Http.Dispatcher;
using System.Web.Http.Services;

using System.Web.Mvc;


namespace SharpLite.NHibernateProvider.WebApi
{
/// <summary>
/// Taken from http://nhforge.org/blogs/nhibernate/archive/2011/03/03/effective-nhibernate-session-management-for-web-apps.aspx
/// </summary>
public class SessionPerRequestModule : IHttpModule
{
public void Init(HttpApplication context) {
context.BeginRequest += ContextBeginRequest;
context.EndRequest += ContextEndRequest;
context.Error += ContextError;
}

private void ContextBeginRequest(object sender, EventArgs e) {
foreach (var sessionFactory in GetSessionFactories()) {
var localFactory = sessionFactory;

LazySessionContext.Bind(new Lazy<ISession>(() => BeginSession(localFactory)), sessionFactory);
}
}

private static ISession BeginSession(ISessionFactory sessionFactory) {
var session = sessionFactory.OpenSession();
session.BeginTransaction();
return session;
}

private void ContextEndRequest(object sender, EventArgs e) {
foreach (var sessionfactory in GetSessionFactories()) {
var session = LazySessionContext.UnBind(sessionfactory);
if (session == null) continue;
EndSession(session);
}
}

private void ContextError(object sender, EventArgs e) {
foreach (var sessionfactory in GetSessionFactories()) {
var session = LazySessionContext.UnBind(sessionfactory);
if (session == null) continue;
EndSession(session, false);
}
}

private static void EndSession(ISession session, bool commitTransaction = true) {
try {
if (session.Transaction != null && session.Transaction.IsActive) {
if (commitTransaction) {
try {
session.Transaction.Commit();
}
catch {
session.Transaction.Rollback();
throw;
}
}
else {
session.Transaction.Rollback();
}
}
}
finally {
if (session.IsOpen)
session.Close();

session.Dispose();
}
}

public void Dispose() { }

/// <summary>
/// Retrieves all ISessionFactory instances via IoC
/// </summary>
private IEnumerable<ISessionFactory> GetSessionFactories() {
var sessionFactories = GlobalConfiguration.Configuration.DependencyResolver
.GetServices(typeof(ISessionFactory)).Cast<ISessionFactory>();

if (sessionFactories == null || !sessionFactories.Any())
throw new TypeLoadException("At least one ISessionFactory has not been registered with IoC");

return sessionFactories;
}
}
}