我正在使用PerWebRequest lifestyle注册一些与Linq2Sql相关的组件。我看到它们被创建,但它们在我的全局的Application_EndRequest方法被调用之前就被销毁了。这是设计出来的吗?有没有人知道这附近有什么工作?我想在我的UnitOfWork对象上调用commit,以便在每次请求结束时提交更改()。除了使用Global.asax Application_EndResult之外,我还尝试了IHttpModule,并获得了相同的结果。
我使用的是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 ( CSL和Windsor Adapter都是1.0)从EndRequest中提取UoW,如下所示:
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();
}
}谢谢,科里
发布于 2009-12-31 04:34:22
尝试将您的Application_EndRequest代码移动到一个before模块,并在PerWebRequestLifestyleModule之前注册它。
发布于 2010-07-28 13:55:36
您的IUnitOfWorkManager实现应该实现IDisposable,并在Dispose中调用SubmitChanges。或者,使用自定义退役提交更改关注。
https://stackoverflow.com/questions/1956530
复制相似问题