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]>

> 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]
> [email protected]
> [email protected]
> 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].
> 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.
>
>
>

-- 
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