[ASP.NET-MVC3] How to manage CurrentSessionContext in a multi-threaded controller action

472 views
Skip to first unread message

Marcello Esposito

unread,
Nov 28, 2012, 5:57:19 AM11/28/12
to nhu...@googlegroups.com
Hi all.

I have a ASP.NET MVC3 application using the Transaction ASP.NET MVC action filter from NH 3 Cookbook, so current_session_context_class="web".
Everything works fine except in a controller action method which uses multi-threading.
I cannot figure out how to initialize current session context used by injected repositories (used within the threads).

Each thread has the SessionFactory singleton injected and executes:

using (var session = sessionFactory.OpenSession())
{
    CurrentSessionContext.Bind(session); <--- this throws NullReferenceException

Any help?

Thanks in advance,
Marcello.

Øyvind Valland

unread,
Nov 28, 2012, 6:02:26 AM11/28/12
to nhu...@googlegroups.com
Can you show us the code of the controller in question?


--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To view this discussion on the web visit https://groups.google.com/d/msg/nhusers/-/kbuTKPELosMJ.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.



--
Visit my blog at http://www.babel-lutefisk.net

Jason Meckley

unread,
Nov 28, 2012, 6:21:43 AM11/28/12
to nhu...@googlegroups.com
if you are using the "web" session context, but you are trying to bind the NH session outside of an HTTP request (which you are in a multi-threaded scenario) it will throw a NRE because there is no HTTP context to store the session (HttpContext.Current.Items[key] = session). Not sure if one exists, but you would want to use a hybrid context. or use an IoC container to manage the scope of the session rather than the SessoinFactory.

Marcello Esposito

unread,
Nov 28, 2012, 6:45:10 AM11/28/12
to nhu...@googlegroups.com
Thanks Jason.

I'm using Ninject as IoC container. But I can't figure out how to configure it in such a hybrid scenario. 
I know it is not that easy to explain with so little information. But even just a link would help.

Thanks,
Marcello.

Marcello Esposito

unread,
Nov 28, 2012, 6:53:50 AM11/28/12
to nhu...@googlegroups.com
The action method is:

[NeedsPersistence]
public void ExecuteScheduledTasks()
{
    scheduler.Execute();
}

Scheduler.Execute() is more or less:

                var tasksToBeExecuted = getTask.GetTasks();
                
                foreach (var task in tasksToBeExecuted)
                {
                    var t = threadFactory.Create(task);
                    t.Start();
                }

threadFactory.Create(task) creates (by using (with Ninject.Extensions.Factorya system thread and injects in it sessionFactory and needed repositories and EQO (all using sessionFactory.GetCurrentSession() to access the session).
t.Start() triggers thread execution.

Thread's code is that in my original question.

Thanks.
Marcello.

Jason Meckley

unread,
Nov 28, 2012, 8:22:55 AM11/28/12
to nhu...@googlegroups.com

Marcello Esposito

unread,
Dec 3, 2012, 12:11:26 PM12/3/12
to nhu...@googlegroups.com
Hi Jason.

Thank you for your helpful link.
I will try to follow this approach.

Marcello.
Reply all
Reply to author
Forward
0 new messages