title: NHibernate.HibernateException: No session bound to the current context
body: <p>i'm working in a new project and i'm trying to implement nhibernate with ninject in asp.net mvc. I'm having the error: </p>
  
  <p>NHibernate.HibernateException: No session bound to the current context</p>
  
  <p>Below is the code i'm using:</p>
  
  <pre><code>using FluentNHibernate.Automapping;
  using FluentNHibernate.Cfg;
  using FluentNHibernate.Cfg.Db;
  using FluentNHibernate.Conventions.Helpers;
  using NHibernate;
  using NHibernate.Context;
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  
  namespace TestNhibernateSessions
  {
      public class SessionFactory : IHttpModule
      {
          private static readonly ISessionFactory _SessionFactory;
          static SessionFactory()
          {
              _SessionFactory = CreateSessionFactory();
          }
          public void Init(HttpApplication context)
          {
              context.BeginRequest += BeginRequest;
              context.EndRequest += EndRequest;
          }
          public void Dispose()
          {
          }
          public static ISession GetCurrentSession()
          {
              return _SessionFactory.GetCurrentSession();
          }
  
          private static void BeginRequest(object sender, EventArgs e)
          {
              ISession session = _SessionFactory.OpenSession();
              session.BeginTransaction();
              CurrentSessionContext.Bind(session);
          }
  
          private static void EndRequest(object sender, EventArgs e)
          {
              ISession session = CurrentSessionContext.Unbind(_SessionFactory);
              if (session == null) return;
              try
              {
                  session.Transaction.Commit();
              }
              catch (Exception)
              {
                  session.Transaction.Rollback();
              }
              finally
              {
                  session.Close();
                  session.Dispose();
              }
          }
  
          private static ISessionFactory CreateSessionFactory()
          {
              return Fluently.Configure()
                             .Database(MsSqlConfiguration.MsSql2008.ConnectionString(c =&gt; c.FromConnectionStringWithKey("DefaultConnection")))
                             .Mappings(m =&gt; m.AutoMappings.Add(CreateMappings()))
                             .CurrentSessionContext&lt;WebSessionContext&gt;()
                             .BuildSessionFactory();
          }
  
          private static AutoPersistenceModel CreateMappings()
          {
              return AutoMap
                  .Assembly(System.Reflection.Assembly.GetCallingAssembly())
                  .Where(t =&gt; t.Namespace != null &amp;&amp; t.Namespace.EndsWith("Domain"))
                  .Conventions.Setup(c =&gt; c.Add(DefaultCascade.SaveUpdate()));
          }
      }
  }
  
  [assembly: WebActivatorEx.PreApplicationStartMethod(typeof(TestNhibernateSessions.App_Start.NinjectWebCommon), "Start")]
  [assembly: WebActivatorEx.ApplicationShutdownMethodAttribute(typeof(TestNhibernateSessions.App_Start.NinjectWebCommon), "Stop")]
  
  namespace TestNhibernateSessions.App_Start
  {
      using System;
      using System.Web;
  
      using Microsoft.Web.Infrastructure.DynamicModuleHelper;
  
      using Ninject;
      using Ninject.Web.Common;
      using TestNhibernateSessions.Repository;
      using TestNhibernateSessions.Service;
      using NHibernate;
  
  
      public static class NinjectWebCommon
      {
          private static readonly Bootstrapper bootstrapper = new Bootstrapper();
  
          /// &lt;summary&gt;
          /// Starts the application
          /// &lt;/summary&gt;
          public static void Start()
          {
              DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
              DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
              bootstrapper.Initialize(CreateKernel);
          }
  
          /// &lt;summary&gt;
          /// Stops the application.
          /// &lt;/summary&gt;
          public static void Stop()
          {
              bootstrapper.ShutDown();
          }
  
          /// &lt;summary&gt;
          /// Creates the kernel that will manage your application.
          /// &lt;/summary&gt;
          /// &lt;returns&gt;The created kernel.&lt;/returns&gt;
          private static IKernel CreateKernel()
          {
              var kernel = new StandardKernel();
              try
              {
                  kernel.Bind&lt;Func&lt;IKernel&gt;&gt;().ToMethod(ctx =&gt; () =&gt; new Bootstrapper().Kernel);
                  kernel.Bind&lt;IHttpModule&gt;().To&lt;HttpApplicationInitializationHttpModule&gt;();
  
                  RegisterServices(kernel);
                  return kernel;
              }
              catch
              {
                  kernel.Dispose();
                  throw;
              }
          }
  
          /// &lt;summary&gt;
          /// Load your modules or register your services here!
          /// &lt;/summary&gt;
          /// &lt;param name="kernel"&gt;The kernel.&lt;/param&gt;
          private static void RegisterServices(IKernel kernel)
          {
  
  
  
              kernel.Bind&lt;IProductRepository&gt;().To&lt;ProductRepository&gt;();
              kernel.Bind&lt;IProductService&gt;().To&lt;ProductService&gt;();
  
  
          }
      }
  }
  </code></pre>