Login

Username

Password





Register   Reset password

Get Cuyahoga at SourceForge.net. Fast, secure and Free Open Source software downloads

Forum

Welcome Guest Search | Active Topics | Members

exception while using Thread and NHibernate Options
goolz
Posted: Thursday, July 30, 2009 12:54:29 PM
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:

martijnb
Posted: Friday, July 31, 2009 12:39:30 AM

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()).
goolz
Posted: Saturday, August 1, 2009 4:39:41 AM
Rank: Advanced Member
Groups: Member

Joined: 6/18/2007
Posts: 39
Points: 117
Thanks Martijn
kmoo01
Posted: Tuesday, June 15, 2010 12:59:20 PM
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?
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

Yet Another Forum.net version 1.9.0 running under Cuyahoga.
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.