Robson Amaral

Robson Amaral

  • NA
  • 132
  • 17.3k

No session bound to the current context

Apr 20 2018 10:07 AM
Friends, I have the following problem, when compiling my application it returns me the 
following error: No session bound to the current context. I use NhibernateHelper, and 
the error appears in this line of code:
  1. public static ISession GetSessao()  
  2.        {  
  3.            return Instance.GetSessionFactory().GetCurrentSession();  
  4.        }  
Como posso resolver esse erro?

Segue meu código completo:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using NHibernate.Mapping.Attributes;  
  6. using NHibernate.Cfg;  
  7. using NHibernate;  
  8. using System.IO;  
  9. using NHibernate.Context;  
  10.   
  11. namespace SDPJDominio.DataAccess  
  12. {  
  13.     public class NhibernateHelper  
  14.     {  
  15.         private ISessionFactory sessionFactory;          
  16.           
  17.         public static ISession GetSessao()  
  18.         {  
  19.             return Instance.GetSessionFactory().GetCurrentSession();  
  20.         }  
  21.   
  22.         public static ISessionFactory Teste()  
  23.         {  
  24.             MemoryStream stream = new System.IO.MemoryStream();  
  25.             HbmSerializer.Default.Validate = true;  
  26.             HbmSerializer.Default.Serialize(stream, System.Reflection.Assembly.GetExecutingAssembly());  
  27.             stream.Position = 0;  
  28.             Configuration configuration= new Configuration();              
  29.             configuration.Configure();  
  30.             configuration.SetProperty("connection.connection_string", ConfigNhibernate.getConfig());   
  31.             configuration.SetProperty("current_session_context_class""thread_static");   
  32.             configuration.AddInputStream(stream);  
  33.             stream.Close();  
  34.   
  35.             Instance.sessionFactory = configuration.BuildSessionFactory();  
  36.             return Instance.sessionFactory;  
  37.         }  
  38.          
  39.         public NhibernateHelper()  
  40.         {                      
  41.             MemoryStream stream = new System.IO.MemoryStream();  
  42.             HbmSerializer.Default.Validate = true;  
  43.             HbmSerializer.Default.Serialize(stream, System.Reflection.Assembly.GetExecutingAssembly());  
  44.             stream.Position = 0;  
  45.             Configuration configuration= new Configuration();  
  46.             configuration.Configure();  
  47.             configuration.SetProperty("connection.connection_string", ConfigNhibernate.getConfig());                   
  48.             configuration.AddInputStream(stream);  
  49.             stream.Close();  
  50.                   
  51.             sessionFactory = configuration.BuildSessionFactory();              
  52.         }  
  53.   
  54.         public static ISessionFactory SessionFactory  
  55.         {  
  56.             get { return Instance.sessionFactory; }  
  57.         }  
  58.   
  59.         private ISessionFactory GetSessionFactory()  
  60.         {  
  61.             return sessionFactory;  
  62.         }  
  63.   
  64.         public static NhibernateHelper Instancia  
  65.         {  
  66.             get   
  67.             {  
  68.                 returnCreateSessionKey.gerenciadorDeSessao;   
  69.             }  
  70.         }  
  71.   
  72.         public static ISession OpeningSession()  
  73.         {  
  74.             return Instance.GetSessionFactory().OpenSession();  
  75.         }  
  76.   
  77.         public static void ReloadSession(object obj)  
  78.         {  
  79.             CurrentSession.Refresh(obj);  
  80.         }  
  81.   
  82.         public static ISession ReloadSession  
  83.         {  
  84.             get  
  85.             {  
  86.                 return Instance.GetSessionFactory().GetCurrentSession();  
  87.             }  
  88.         }  
  89.   
  90.         class CreateSessionKey  
  91.         {  
  92.             internal static readonly NhibernateHelper SESSIONMANAGER= new NhibernateHelper();  
  93.         }     
  94.     }  
  95. }  
Could you kindly help me?

Answers (2)