Login

Username

Password





Register   Reset password

This is a read-only archive of the forum.

# goolz 7/30/2009 9:54 PM
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:

# martijnb 7/31/2009 9:39 AM
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:
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()).
# goolz 8/1/2009 1:39 PM
Thanks Martijn
# kmoo01 6/15/2010 9:59 PM
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?