Here are my 5 cents!

I have this on my web.config:

<property 
name="current_session_context_class">NHibernate.Context.WebSessionContext, 
NHibernate</property>

You can't use managed web or any other context, they won't work properly.

On the Application_Start method I create an ISessionFactory and I register 
it on my IoC container so that it is available anywhere through the Common 
Service Locator.
Then on the Application_BeginRequest method I have something like this:

if (this.NeedsProcessing == true) //check if the request is for dynamic 
content, for example, not images, scripts, css, etc
{
ISession session = 
ServiceLocator.Current.GetInstance<ISessionFactory>().OpenSession();
session.BeginTransaction();
WebSessionContext.Bind(session);
}

Next, my Application_EndRequest has this:

if (this.NeedsProcessing == true)
{
ISessionFactory factory = 
ServiceLocator.Current.GetInstance<ISessionFactory>();
ISession session = WebSessionContext.Unbind(factory);

if (session != null)
{
if ((session.Transaction != null) && (session.Transaction.IsActive == true) 
&& (session.Transaction.WasCommitted == false) && 
(session.Transaction.WasRolledBack == false))
{
session.Transaction.Commit();
session.Transaction.Dispose();
}

session.Dispose();
}
}

And finally, most people forget about Application_Error, if there is an 
error, Application_EndRequest is not called:

ISessionFactory factory = 
ServiceLocator.Current.GetInstance<ISessionFactory>();
ISession session = WebSessionContext.Unbind(factory);

if (session != null)
{
if ((session.Transaction != null) && (session.Transaction.IsActive == true) 
&& (session.Transaction.WasCommitted == false) && 
(session.Transaction.WasRolledBack == false))
{
session.Transaction.Rollback();
session.Transaction.Dispose();
}

session.Dispose();
}

RP


On Wednesday, July 3, 2013 8:12:06 PM UTC+1, Gunnar Liljas wrote:
>
> I believe you posted the wrong code for BeginRequest, but other than that, 
> yur code is just fine. I guess you have encapsulated the access to the 
> current session, somewhere in your Controllers/Pages, so you don't have to 
> go to System.Web.HttpContext.Current.Items["NHCurrentSession"] all the 
> time. 
>
> This is exactly how the managed web session context works.
>
> No, you can NOT use thread static access to the session in ASP.NET.
>
> If you really want to build a windows forms application on the same 
> architecture, you should either refrain from doing it (best option), let 
> the methods open their own sessions (just access the session factory), pass 
> sessions as arguments into the methods (double dispatch) or use the 
> CurrentSessionContext.
>
>
> http://pwigle.wordpress.com/2008/11/21/nhibernate-session-handling-in-aspnet-the-easy-way/
>
> /G
>
>
>
>
>
>
>
> 2013/7/3 vitor rubio <[email protected] <javascript:>>
>
>> Hello!
>> I have an Asp.Net WebForms application, with nhibernate 3.3, and i'm 
>> trying to do an efective session per request control, using best practices 
>> and so on. 
>>
>> Untill yesterday i was building my session factory upon the 
>> Application_Start event, in global.asax, and controlling the session in the 
>> Begin_request and End_Request events, like this:
>>
>>         public void Application_BeginRequest(object sender, EventArgs e)
>>         {
>>             ISession currentSession;
>>             if 
>> (System.Web.HttpContext.Current.Items.Contains("NHCurrentSession"))
>>             {
>>                 currentSession = 
>> (ISession)System.Web.HttpContext.Current.Items["NHCurrentSession"];
>>                 if (currentSession.Transaction.IsActive)
>>                 {
>>                     currentSession.Transaction.Rollback();
>>                     currentSession.Transaction.Dispose();
>>                 }
>>                 currentSession.Dispose();
>>                 
>> System.Web.HttpContext.Current.Items.Remove("NHCurrentSession");
>>             }
>>         }
>>
>>         public void Application_EndRequest(object sender, EventArgs e)
>>         {
>>             ISession currentSession;
>>             if 
>> (System.Web.HttpContext.Current.Items.Contains("NHCurrentSession"))
>>             {
>>                 currentSession = 
>> (ISession)System.Web.HttpContext.Current.Items["NHCurrentSession"];
>>                 if(currentSession.Transaction.IsActive)
>>                 {
>>                     currentSession.Transaction.Commit();
>>                     currentSession.Transaction.Dispose();
>>                 }
>>                 currentSession.Dispose();
>>                 
>> System.Web.HttpContext.Current.Items.Remove("NHCurrentSession");
>>             }
>>
>>         }
>>
>>
>> I'd like to know a better way to do this, and how to configure this in 
>> the web.config / hibernate
>>
>>
>> I tried something different, using CurrentSessionContext, but it works 
>> only when I use the configuration <property 
>> name="current_session_context_class">thread_static</property>. When i try 
>> <property name="current_session_context_class">managed_web</property> it 
>> fails. 
>>
>> Can I use thread_static in the web?
>>
>> I heard about unhaddins, but I couldn't find an easy to follow example 
>> using webforms, only wcf, and i was wondering if anhaddins is compatible 
>> with nh 3.3. 
>>
>> My second option is building a webmodule, but i don't know how to.
>> My final goal is to use in a windows form application the same 
>> architeture i'm using in the web. 
>> []s 
>>  *Vitor Luiz Rubio* \( ^^ ' )/ 
>> Arquiteto de Software|Analista de Sistemas Sr.|Programador 
>>  [email protected] <javascript:>
>> [email protected] <javascript:>
>> [email protected] <javascript:>
>> Cel. +55 (11) 97992-4544 
>>  Contatos <http://www.meadiciona.com/vitorrubio> | 
>> Blog<http://vitorrubio.blogspot.com/>
>>  
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "nhusers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected] <javascript:>.
>> To post to this group, send email to [email protected]<javascript:>
>> .
>> Visit this group at http://groups.google.com/group/nhusers.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>  
>>  
>>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/nhusers.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to