Attempt to implement a session for each web request, lack of the current configure session context

As I said in the header, I want to implement a session for each web request. My session provider is configured as follows (I'm not interested in changing this conf.)

    public class SessionProvider
    {
        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 mappings 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();
        }
    }

Inside Global.asax.cs I have the following code related to a session on a website.

private static ISessionFactory SessionFactory { get; set; }

    protected void Application_Start()
    {
        SessionFactory = MyDomain.SessionProvider.Instance.OpenSession().SessionFactory;

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

    }

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var session = SessionFactory.OpenSession();
        CurrentSessionContext.Bind(session);
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        var session = CurrentSessionContext.Unbind(SessionFactory);
        session.Dispose();
    }

In the debuggin of my webapp, I get the error message: The current session context is not configured. ErrorMessage refers to the string global.asax CurrentSessionContext.Bind (session);

Update Added .ExposeConfiguration(c => c.SetProperty("current_session_context_class", "web")) and now I have an error message while retrieving data from my controller, like this Error message: Session closed! Object Name: "ISession".

Controller Code:

using (ISession session = SessionProvider.Instance.GetCurrentSession())
            {
                using (ITransaction transaction = session.BeginTransaction())
                {
                    data = session.QueryOver<Object>()
                       ...ommiting

                    transaction.Commit();
                    return PartialView("_Partial", data);
                }

            }    
+3
1

1-

nhibernate:

<property name="current_session_context_class">web</property>

- :

if (!CurrentSessionContext.HasBind(SessionFactory))
{
    CurrentSessionContext.Bind(SessionFactory.OpenSession());
}

, , : currentsessioncontext fluent nhibernate,

.

using (ISession session = SessionProvider.Instance.GetCurrentSession()) 

. Application_EndRequest. , .

+5

All Articles