views:

29

answers:

0

I have this web application which uses SM for IoC. I am using HybridHttpOrThreadLocalScoped scope to store my nHibernate ISession objects. This works ok in a session per request fashion for my web requests.

But I also have quartz.net that schedules couple of jobs. The job uses the same unit of work to get the ISession object. In this scenario when the scheduler start the job, everything works fine at first and the job runs fine for couple of times UNTIL the job thread id gets repeated.

Imagine that when the job is scheduled it start to run in threads with ids 11, 12, 13, and then with thread id 11 again. At this point structuremap returns a session object which is already disposed and I get "System.ObjectDisposedException: Session is closed!" error.

So from what I can see, the session is kept in thread local storage and after I dispose the session at the end of my unit of work, the session object is still kept in the thread local storage. It seems that after the thread terminates its local storage is not cleared and somehow when a new thread with the same id is created, structuremap looks up the session in the old thread local storage (which is supposed to be cleared for the new thread I believe) and returns the session object which is already disposed.

Questions:

  1. Is there a way to clear the thread local storage (on termination)?
  2. Is there an equivalent of "ReleaseAndDisposeAllHttpScopedObjects" for thread-scoped objects?
  3. Is there a way to nullify (or eject) the disposed object so even if SM looks for it then it wouldn't find any and has to create a new instance?

I hope I made my question clear. This has taken couple of hours of my time and still I haven't found a way around it. I appreciate any hint :>