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 7976279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:52:35+00:00 2026-06-04T08:52:35+00:00

For example. My session factory is located in MyDomain.SessionProvider class. Session can be open

  • 0

For example.
My session factory is located in MyDomain.SessionProvider class.
Session can be open using ISession session = SessionProvider.Instance.OpenSession()

Step: SessionProvider.cs

public static SessionProvider Instance { get; private set; }
        private static ISessionFactory _SessionFactory;

        static SessionProvider()
        {
            var provider = new SessionProvider();
            provider.Initialize();
            Instance = provider;
        }

        private SessionProvider()
        {

        }

        private void Initialize()
        {
            string csStringName = "ConnectionString";
            var cfg = Fluently.Configure()
               //ommiting mapping and db conf.

                .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web"))
                .BuildConfiguration();
            _SessionFactory = cfg.BuildSessionFactory();

        }

        public ISession OpenSession()
        {
            return _SessionFactory.OpenSession();
        }

        public ISession GetCurrentSession()
        {
            return _SessionFactory.GetCurrentSession();
        }

Step: Global.asax.cs

public static ISessionFactory SessionFactory { get; private set; }

Application Start

SessionFactory = SessionProvider.Instance.OpenSession().SessionFactory;

App_BeginRequest

var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);   

EndRequest
dispose session

var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();

Step3.HomeController
I should be using current session like

var session = SessionProvider.Instance.GetCurrentSession();
using (var tran = session.BeginTransaction())
{
   //retrieve data from session
}

Now, with trying to retrieve data on my controller like desc. in Step3. I got error message that my session is closed. I tried to remove Application_EndRequest block inside global.asax cause my transaction is wrapped with session but with no success. Still same error.

Second/side question: is this pattern accepted widely, or it is better to wrapped inside custom attributes on mvc controllers. Thanks.

Updated:
On my controller when try to instantiate current session in line

var session = SessionProvider.Instance.GetCurrentSession();

I’m getting following error:

**Connection = 'session.Connection' threw an exception of type 'NHibernate.HibernateException'**

**base {System.ApplicationException} = {"Session is closed"}**
  • 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-06-04T08:52:36+00:00Added an answer on June 4, 2026 at 8:52 am

    Thanks @LeftyX

    I solved this problem using TekPub video Mastering NHibernate with some customizations.

    Global.asax

    //Whenever the request from page comes in (single request for a page)
    //open session and on request end close the session.
    
    public static ISessionFactory SessionFactory =
       MyDomain.SessionProvider.CreateSessionFactory();
    
    public MvcApplication() 
    {
        this.BeginRequest += new EventHandler(MvcApplication_BeginRequest);
        this.EndRequest +=new EventHandler(MvcApplication_EndRequest);
    }
    
    private void MvcApplication_EndRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Unbind(SessionFactory).Dispose();
    }
    
    private void MvcApplication_BeginRequest(object sender, EventArgs e)
    {
        CurrentSessionContext.Bind(SessionFactory.OpenSession());
    }
    
    protected void Application_Start()
    {
        SessionFactory.OpenSession();
    }
    

    and inside my controller

     var session = MvcApplication.SessionFactory.GetCurrentSession();
     {
         using (ITransaction tx = session.BeginTransaction())
          {... omitting retrieving data}
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interface ISession whose instance is produced by a different Session Factory
Example: I have an class that inherits from UIImageView. An object creates an instance
I have next code: PhotoFactory factory = PhotoFactory.getFactory (PhotoResource.PICASA); PhotoSession session = factory.openSession (login,
I have one ArrayList in Session, lets say for example [305,306,380] . On submit,
I am trying to simulate a session using FactoryGirl / shoulda (it worked with
Is it possible to create a factory or proxy that can decide if thread
I commonly see people setting $_SESSION variables as $_SESSION['example']=$_REQUEST['something']; $example=$_SESSION['example']; is this redundant? I
When I execute this following code for example: cart.php?p=1&action=add <?php session_start(); if (isset($_GET['p']) &&
For sessions and cookies, is there a difference between example.com and www.example.com? I have
Is there any way of dealing with sessions with sockets in C#? Example of

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.