| Rank: Advanced Member Groups: Member
 
 Joined: 6/18/2007
 Posts: 39
 Points: 117
 
 | 
		    Hi 
 I tried to write a task manager by using Thread class and NHibernate , but I got the following exception.
 I also remove isWeb="true" from facilities.config.
 
 
 Sample Code:
 
 
 private Article _article;
 private ArticleModule _articleModule;
 
 private void StartTask()
 
 {
 
 Thread thread = new Thread(new ThreadStart(Execute));
 
 thread.Start();
 
 
 }
 
 private void Execute()
 
 {
 
 try
 
 {
 this._article = this._articleModule.GetArticleById(articleId);
 
 // ...
 }
 
 catch
 
 {
 
 }
 
 
 }
 
 
 Exception error
 
 
 Castle.MicroKernel.Facilities.FacilityException was unhandled
 Message="WebSessionStore: Could not obtain reference to HttpContext"
 Source="Castle.Facilities.NHibernateIntegration"
 StackTrace:
 at Castle.Facilities.NHibernateIntegration.Internal.WebSessionStore.ObtainSessionContext()
 at Castle.Facilities.NHibernateIntegration.Internal.WebSessionStore.GetDictionary()
 at Castle.Facilities.NHibernateIntegration.Internal.AbstractDictStackSessionStore.GetStackFor(String alias)
 at Castle.Facilities.NHibernateIntegration.Internal.AbstractSessionStore.FindCompatibleSession(String alias)
 at Castle.Facilities.NHibernateIntegration.Internal.DefaultSessionManager.OpenSession(String alias)
 at Castle.Facilities.NHibernateIntegration.Internal.DefaultSessionManager.OpenSession()
 at SAS.Core.CoreModule.GetUndoneTask()
 at SAS.Core.Web.TaskMgmt.execute()
 at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
 at System.Threading.ExecutionContext.runTryCode(Object userData)
 at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
 at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
 at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
 at System.Threading.ThreadHelper.ThreadStart()
 InnerException:
 
 
 | 
	|  Rank: Administration
 Groups: Administration
, Member
 
 Joined: 12/30/2004
 Posts: 1,674
 Points: 1,824
 Location: Wageningen (NL)
 
 | 
		    When performing things in a separate thread, you have to obtain a session manually because as if you have noticed, there is no HttpContext where the session manager retrieves its session from (don't remove isWeb from the config). In your module: Code:
private void Execute()
 {
 ISessionFactory sessionFactory = IoC.Resolve< ISessionFactory >();
 using (ISession session = sessionFactory.OpenSession())
 {
 MyObject myObject = session.Get< MyObject >(objectId);
 ...
 }
 }
 
 See also the RemoteContentModule.cs (FetchFeed()).
		 | 
	| Rank: Advanced Member Groups: Member
 
 Joined: 6/18/2007
 Posts: 39
 Points: 117
 
 | 
		    Thanks Martijn
		 | 
	| Rank: Newbie Groups: Member
 
 Joined: 4/8/2010
 Posts: 7
 Points: 21
 
 | 
		    Hi guys,Forgive the late post, I am playing with the 2 beta and implemented as simular task archetecturem and was wondering how you would do the same, as a lot of the SessionFactory code was replaced with the SessionFactoryHelper and the ISessionManager?
 
 |