Castle Windsor PerWebRequest LifeStyle and Application_EndRequest
Asked Answered
E

2

1

I'm registering some components related to Linq2Sql using PerWebRequest lifestyle. I see them get created, but they get destroyed before my global's Application_EndRequest method gets called. Is that by design? Does anyone know a work around? I want to call commit on my UnitOfWork object to submitchanges() at the end of every request. In addition to using the Global.asax Application_EndResult, I've also tried an IHttpModule with the same results.

I'm using Castle 2.0.

Here's how I'm registering my stuff with PerWebRequest. I am creating a DataCOntextProvider object that holds onto a L2S DataContext. That object is injected into the UoW.

/// <summary>
        /// Register the IUnitOfWorkManager to resolve to LinqToSqlUnitOfWorkManager per web request
        /// </summary>
        public void RegisterLinq2SqlUnitOfWorkPerWebRequest()
        {
            _container.Register(Component.For<IUnitOfWorkManager>()
              .LifeStyle.PerWebRequest
              .ImplementedBy<LinqToSqlUnitOfWorkManager>());
        }

    /// <summary>
    /// Register the IDataContextProvider to resolve to DataContextProvider per web request
    /// </summary>
    public void RegisterDataContextProviderPerWebRequest()
    {
        _container.Register(Component.For<IDataContextProvider>()
          .LifeStyle.PerWebRequest
          .ImplementedBy<DataContextProvider>());
    }

Now I am simply trying to pull the UoW from the container via the CommonServiceLocator (both CSL and Windsor Adapter are 1.0) from the EndRequest like this:

 protected void Application_EndRequest(object sender, EventArgs e)
    {
        //ignore unless this is a page (.aspx) or handler (.ashx)
        if (!RequestCanHaveContext())
            return;

        //get the IUnitOfWork manager
        var uow = ServiceLocator.Current.GetInstance<IUnitOfWorkManager>();

        //if we have one, commit changes at the end of the request
        if (uow != null)
        {
            //don't explicitly dispose of uow or we'll get Disposed exceptions on the context
            uow.Commit();
        }

    }

Thanks, Corey

Embryo answered 24/12, 2009 at 3:39 Comment(2)
groups.google.com/group/castle-project-users/browse_thread/…Utopian
did you verify that the endrequest handler of your module runs before component destruction? Also, where/how are you handling the kernel's destroyed event?Utopian
U
1

Try moving your Application_EndRequest code to a httpmodule and register it before the PerWebRequestLifestyleModule.

Utopian answered 30/12, 2009 at 20:34 Comment(2)
I did do that. I am ending up with very wierd behavior - things getting created/destroyed without firing the Micro Kernel's Destroyed event. I don't know if it ASP.NET that is causing me pain.Embryo
did you verify that the endrequest handler of your module runs before component destruction? Also, where/how are you handling the kernel's destroyed event?Utopian
U
1

your implementation of IUnitOfWorkManager should implement IDisposable and in Dispose call SubmitChanges. Alternatively use custom decommission submit changes concern.

Updo answered 28/7, 2010 at 5:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.