locked
Server Error- No session bound to the current context RRS feed

  • Question

  • User662762443 posted

    Good afternoon friends, I'm getting the following error in my nhibernate class:

    Server Error in '/ 'Application.

    No session bound to the current context.

    I already looked at some forums but I still could not solve the problem of the exception thrown.

    [HibernateException: No session bound to the current context]

    Nhibernate.Context.CurrentSessionContext.CurrentSession() +75
    Nhibernate.Imp.SessionFactoryImpl.GetCurrentSession() +16
    Domain.DataAccess.NhibernateHelper.get_CurrentSession() in D:\Program Files (x86)\...\...\DataAccess\NhibernateHelper.cs: 96
    Domain.DataAccess.CityDAO.ListCity() in D:\Program Files (x86)\...\...\...\DataAccess\CityDAO.cs: 15
    Domain.APL.aplCity.listAll() in D:\Program Files (x86)\...\...\...\APL\AplCity.cs: 18

    NhibernateHelper.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using NHibernate.Mapping.Attributes;
    using NHibernate.Cfg;
    using NHibernate;
    using System.IO;
    using NHibernate.Context;
    using System.Reflection;
    using Oracle.DataAccess.Client;
    
    namespace SDPJDominio.DataAccess
    {
        public class NhibernateHelper
        {
            private ISessionFactory sessionFactory;
    
    
            public static ISession GetSessao ()
            {
                // if (! CurrentSessionContext.HasBind (Instance.GetSessionFactory ()))
                // CurrentSessionContext.Bind (Instance.GetSessionFactory (). OpenSession ());
    
                return Instance.GetSessionFactory (). GetCurrentSession ();
            }
    
            public static ISessionFactory HelpThreading ()
            {
                MemoryStream stream = new System.IO.MemoryStream ();
                HbmSerializer.Default.Validate = true;
                ////HbmSerializer.Default.Serialize(stream, AppDomain.CurrentDomain.GetAssemblies (). Where (w => w.FullName.Contains ("SDPJDomain").) FirstOrDefault ());
                HbmSerializer.Default.Serialize(stream, System.Reflection.Assembly.GetExecutingAssembly ());
                //stream.Position = 0;
                 Configuration configuration = new Configuration ();
                configuration.configure ();
                configuration.SetProperty("connection.connection_string ", ConfigNhibernate.getConfig ());
                configuration.SetProperty("current_session_context_class "," thread_static ");
                configuration.AddInputStream(stream);
                stream.Close ();
                Instance.sessionFactory = configuration.BuildSessionFactory ();
                return Instance.sessionFactory;
                return null;
            }
    
    
    
    public NhibernateHelper ()
            {
                MemoryStream stream = new System.IO.MemoryStream ();
                HbmSerializer.Default.Validate = true;
                HbmSerializer.Default.Serialize (stream, System.Reflection.Assembly.GetExecutingAssembly ());
                stream.Position = 0;
                Configuration configuration = new Configuration ();
                configuration.Configure ();
                configuration.SetProperty ("connection.connection_string", ConfigNhibernate.getConfig ()); // add this line
                configuration.AddInputStream (stream);
                stream.Close ();
    
                sessionFactory = configuration.BuildSessionFactory ();
            }
    
            public static ISessionFactory SessionFactory
            {
                get {return Instance.sessionFactory; }
            }
    
            private ISessionFactory GetSessionFactory ()
            {
                return sessionFactory;
            }
    
            public static NhibernateHelper Instance
            {
                get
                {
                    return CreateSessionGenerator.
                }
            }
    
    
    public static ISession OpenSession ()
            {
                return Instance.GetSessionFactory (). OpenSession ();
            }
    
            public static void ReloadSession (object obj)
            {
                SessionRefresh (obj);
            }
    
            public static ISession SessionCurrent
            {
                get
                {
                   
                    // if (! ManagedWebSessionContext.HasBind (Instance.GetSessionFactory ()))
                    // return null;
                    return Instance.GetSessionFactory (). GetCurrentSession ();
                }
            }
    
            class CreateSessionKey
            {
                internal static readonly NhibernateHelperSession Manager = new NhibernateHelper ();
            }
    
    
        }
    }
    

    My class CityDAO:

    public class
        {
            public static List <City> ListCity ()
            {
                ISession session = NhibernateHelper.CurrentSession;
             
                try
                {
                    
                    ICriteria criteria = session.CreateCriteria (typeof (City));
                    criteria.AddOrder (Order.Asc ("Code"));
                    return criteria.List <City> (). ToList ();
                }
                catch (Exception e)
                {
                    throw new Exception (e.ToString ());
                }
            }
    
        }

    My class AplCity:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Domain;
    using Domain.DataAccess;
    
    
    namespace Domain.APL
    {
       public class AplCity
        {
    
         
    public List <City> listAll ()
           {
               return CityDAO.ListCity ();
           }
    
    
        }
    }

    This error is taking my sleep, I do not know what to do to correct this problem. Would anyone have any examples of what they might be doing?

    Tuesday, October 2, 2018 5:26 PM

Answers