问题描述
我注册使用PerWebRequest生活方式有关LINQ2SQL某些组件。我看到他们生成,但我全球的Application_EndRequest方法被调用之前被摧毁。是设计?有谁知道周围的工作?我想打电话给我的UnitOfWork对象承诺的SubmitChanges()在每一个请求结束。除了使用在Global.asax Application_EndResult,我也试过用IHttpModule的相同的结果。
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.
我使用的是城堡2.0。
I'm using Castle 2.0.
下面就是我如何注册我的内容PerWebRequest。我创建保存到L2S的DataContext一个DataCOntextProvider对象。该对象被注入UOW
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>());
}
现在我只是想通过CommonServiceLocator从容器拉UOW(包括CSL和温莎适配器是1.0)从EndRequest是这样的:
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
推荐答案
您实施 IUnitOfWorkManager
应实施的IDisposable
在处置通话的SubmitChanges。或者使用自定义的退役提交变化的关注。
your implementation of IUnitOfWorkManager
should implement IDisposable
and in Dispose call SubmitChanges. Alternatively use custom decommission submit changes concern.
这篇关于温莎城堡PerWebRequest生活方式和Application_EndRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!