3

I am getting this error when I am trying to get the CurrentSession

NHibernate.Context.CurrentSessionContext.CurrentSession()

at

NHibernate.Impl.SessionFactoryImpl.GetCurrentSession()

3 Answers 3

13

Like David M said, you need to make sure you are binding your NHibernate session. Here's the way I do it right now in my ASP.NET app:

public class NHHttpModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.EndRequest += ApplicationEndRequest;
        context.BeginRequest += ApplicationBeginRequest;
    }

    public void ApplicationBeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(NHSessionFactory.GetNewSession());
    }

    public void ApplicationEndRequest(object sender, EventArgs e)
    {
        ISession currentSession = CurrentSessionContext.Unbind(
            NHSessionFactory.GetSessionFactory());
        currentSession.Close();
        currentSession.Dispose();
    }

    public void Dispose()
    {
        // Do nothing
    }
}

I create a custom HttpModule that binds my session on every request and then I add this module to my web.config like this:

<httpModules>
  <add name="NHHttpModule" type="MyApplication.Core.NHHttpModule, MyApplication,
  Version=1.0.0.0, Culture=neutral" />      
</httpModules>

I'm sure your configuration is different then this but this is just an example of how I bind my session. Hope this helps a little.

Sign up to request clarification or add additional context in comments.

Comments

11

Studio 2010 will create 2 httpModules sections, one is for IIS 7. Be sure to register your nhibernate httpmodule in the system.web one too.

3 Comments

Genius, thanks for this answer, saved me from a massive 'egg on face' incident :)
I had to put mine in the modules here for it to work: <system.webServer> <modules></modules> </system.webServer>
you need to add your HttpModule in web.config in these section <system.web><module></system.web> and <system.webServer><module></system.webServer>
6

You are responsible for setting the current session on the session context. See this section of the NHibernate documentation. If you haven't done this, then there will be no current session to retrieve.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.