views:

399

answers:

1

I have the following code in my application start method along with the code to setup the container and factory support. The factory method seems to only be called once instead of every time the session is needed.

_container.Kernel.Register(
    Component.For<ISession>().LifeStyle.Transient
    .UsingFactoryMethod(() =>
    {
        //Any line of code will only be hit once the first time ISession is requested.
    }));

I was initially using build 1509 from the trunk, but updated to build 1548 with the same result. Please let me know if you need more information to help me.

Note: This is cross posted to the castle mailing list awaiting approval.

Note 2: This code works with the latest release version of windsor. However, we are working off of the trunk build because we needed the functionality from changeset 5927 (created by mausch on 13 August 2009).

See:

  1. http://stackoverflow.com/questions/1784653/register-multiple-components-for-single-interface-using-castle-windsor
  2. http://fisheye2.atlassian.com/changelog/castleproject/?cs=5927
+1  A: 

why the heck are you not using PerWebRequest lifetime for this component, or better yet Windsor's NHibernate integration facility?

Now back to the real problem.

Your code does not work because it has bug in it. You're not storing the reference in a current HttpContext. You're storing a reference in a HttpContext that is current to the first call to the delegate you passed. The anonymous delegate (or anonymous class that encompasses it) stores the reference to that first instance of HttpContext, so your if condition will be false only the very first time the request arrives.

Krzysztof Koźmic
I cannot use PerWebRequest as it bombs out when I use it in application start. I modified the question to so that when I step through the code in the debugger, the first line of code in the Factory Method is only hit the first time ISession is requested and never again. It seems like the transient lifestyle is not working.
beckelmw
if you're suggesting that .UsingFactoryMethod with conjunction with Lifestyle.Transient is not working, than you're wrong. I just tested it on the trunk.
Krzysztof Koźmic
again, why won't you use NHibernate integration facility? I'm sure it can handle scenarios like your application start.
Krzysztof Koźmic
Updated question. Code works against release, but not trunk version 1548 which I show is the latest that builds?
beckelmw
Krzysztof Koźmic
For ways to get PerWebRequest to work when you define the container in application_start, see if this helps: http://blog.ploeh.dk/2009/11/17/UsingCastleWindsorsPerWebRequestLifestyleWithASPNETMVCOnIIS7.aspx
Mark Seemann
+1 for suggesting PerWebRequest
Mark Seemann