Windsor Castle PerWebRequest LifeStyle and Application_EndRequest

I am registering some components related to Linq2Sql using the PerWebRequest style. I see that they are created, but they are destroyed before calling the global Application_EndRequest method. Is it for design? Does anyone know a job? I want to call commit on my UnitOfWork object for subscriptions () at the end of each request. In addition to using the Global.asax Application_EndResult application, I also tried IHttpModule with the same results.

I am using Castle 2.0.

This is how I register my material with PerWebRequest. I am creating a DataCOntextProvider object that is stored in the L2S DataContext. This object is entered in 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'm just trying to pull the UoW out of the container via the CommonServiceLocator (both CSL and Windsor Adapter 1.0) from EndRequest as follows:

  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

+1
castle-windsor global-asax
source share
2 answers

your implementation of IUnitOfWorkManager should implement IDisposable and call SubmitChanges in Dispose. Alternatively, you can use the changes associated with making changes to the shutdown.

+1
source share

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

0
source share

All Articles