Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 564661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:47:25+00:00 2026-05-13T12:47:25+00:00

I’m currently building an ASP.NET MVC project, with NHibernate as its persistance layer. For

  • 0

I’m currently building an ASP.NET MVC project, with NHibernate as its persistance layer.

For now, some functionality have been implemented, but only use local NHibernate sessions: each method that accessed the database (read or write) needs to instantiate its own NHibernate session, with the “using()” clause.

The problem is that I want to leverage NHibernate’s lazy-loading capabilities to improve the performance of my project.

This implies an open NHibernate session per request until the view is rendered. Furthermore, simultaneous requests must be supported (multiple Sessions at the same time).

How can I achieve that as cleanly as possible?

I searched the web a little bit and learned about the session-per-request pattern. Most of the implementations I saw used some sort of Http* (HttpContext, etc.) object to store the session. Also, using the Application_BeginRequest/Application_EndRequest functions is complicated, since they get fired for each HTTP request (aspx files, css files, js files, etc.), when I only want to instantiate a session once per request.

The concern that I have is that I don’t want my views or controllers to have access to NHibernate sessions (or, more generally, NHibernate namespaces and code). That means that I do not want to handle sessions at the controller level nor the view one.

I have a few options in mind. Which one seems the best?

  • Use interceptors (like in GRAILS) that get triggered before and after the controller action. These would open and close sessions/transactions. Is this possible in the ASP.NET MVC world?
  • Use the CurrentSessionContext Singleton provided by NHibernate in a Web context. Using this page as an example, I think this is quite promising, but that still requires filters at the controller level.
  • Use the HttpContext.Current.Items to store the request session. This, coupled with a few lines of code in Global.asax.cs, can easily provide me with a session on the request level. However, it means that dependencies will be injected between NHibernate and my views (HttpContext).

Thank you very much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:47:26+00:00Added an answer on May 13, 2026 at 12:47 pm

    Well guys, after a few days’ work, I finally decided to use the HttpContext.Current.Items to load the session.

    It works great!

    Here’s how I did it

    import System.Web
    class SessionManager {
        public static ISession GetSession()
            var session = HttpContext.Current.Items["NHibernateSession"];
            if (session == null) {
                session = ...; // Create session, like SessionFactory.createSession()...
                HttpContext.Current.Items.Add("NHibernateSession", session);
            }
            return session;
        }
    
        public static void CloseSession()
        {
            var session = HttpContext.Current.Items["NHibernateSession"];
            if (session != null) {
                if (session.IsOpen) {
                    session.close();
                }
                HttpContext.Current.Items.Remove("NHibernateSession");
            }
        }
    }
    

    By using the static methods provided by this class, one can get a session (for example, in a Controller) that is tied to the current HttpContext (the current Web request). We need another snippet of code to call the CloseSession() method when the request is completed.

    In Global.asax.cs:

    protected void Application_EndRequest(object sender, EventArgs args)
    {
        NHibernateSessionManager.CloseSession();
    }
    

    The Application_EndRequest event is automatically called when the session is completed, so the session can be properly closed an disposed of. This is useful, because otherwise we would have to do this in every Controller!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.