温莎城堡 PerWebRequest 生活方式和 Application_EndRequest
我正在使用 PerWebRequest 生活方式注册一些与 Linq2Sql 相关的组件。我看到它们被创建,但它们在调用我的全局的 Application_EndRequest 方法之前被销毁。这是设计使然吗?有谁知道解决办法吗?我想在每个请求结束时调用 UnitOfWork 对象上的 commit 来提交changes()。除了使用 Global.asax Application_EndResult 之外,我还尝试了具有相同结果的 IHttpModule。
我正在使用城堡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>());
}
现在我只是尝试通过 EndRequest 中的 CommonServiceLocator(CSL 和 Windsor Adapter 都是 1.0)从容器中提取 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();
}
}
谢谢, 科里
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试将您的
Application_EndRequest
代码移至 httpmodule 并在
PerWebRequestLifestyleModule
之前注册它。Try moving your
Application_EndRequest
code to a httpmodule andregister it before the
PerWebRequestLifestyleModule
.您的
IUnitOfWorkManager
实现应该实现IDisposable
并在 Dispose 中调用 SubmitChanges。或者使用自定义停用提交更改问题。your implementation of
IUnitOfWorkManager
should implementIDisposable
and in Dispose call SubmitChanges. Alternatively use custom decommission submit changes concern.