2009-12-24 23 views
1

我正在使用PerWebRequest生活方式註冊一些與Linq2Sql相關的組件。我看到它們被創建,但在全局的Application_EndRequest方法被調用之前它們會被銷燬。這是由設計嗎?有誰知道解決辦法?我想在我的UnitOfWork對象上調用commit來在每個請求結束時提交changes()。除了使用Global.asax Application_EndResult之外,我還嘗試了一個具有相同結果的IHttpModule。Castle Windsor PerWebRequest LifeStyle and Application_EndRequest

我正在使用Castle 2.0。

下面是我如何使用PerWebRequest註冊我的東西。我正在創建一個保存在L2S DataContext上的DataCOntextProvider對象。這個對象被注入到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>()); 
    } 

現在我只是想通過CommonServiceLocator從容器拉UOW(包括CSL和溫莎適配器1.0)從EndRequest這樣的:

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(); 
     } 

    } 

感謝, 科瑞

+0

http://groups.google.com/group/castle-project-users/browse_thread/thread/b8f52a3640ffb654 – 2009-12-26 14:54:06

+0

您是否確認模塊的endrequest處理程序在組件銷燬之前運行?另外,你在哪裏/如何處理內核被銷燬的事件? – 2010-01-06 21:17:50

回答

0

嘗試將您的Application_EndRequest代碼註冊到httpmodule,並在PerWebRequestLifestyleModule之前註冊 。

+0

我的確做到了。我結束了非常奇怪的行爲 - 在沒有觸發Micro Kernel's Destroyed事件的情況下創建/銷燬事物。我不知道ASP.NET是否會給我帶來痛苦。 – 2009-12-31 14:37:59

+0

你是否確認了模塊的endrequest處理程序在組件銷燬之前運行?另外,你在哪裏/如何處理內核被銷燬的事件? – 2009-12-31 21:20:30

1

你的執行IUnitOfWorkManager應執行IDisposable並在處理調用SubmitChanges。或者使用自定義的退役提交更改關注。