首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >城堡温莎和LifeStyle PerWebRequest Application_EndRequest

城堡温莎和LifeStyle PerWebRequest Application_EndRequest
EN

Stack Overflow用户
提问于 2009-12-24 11:39:04
回答 2查看 2.5K关注 0票数 1

我正在使用PerWebRequest lifestyle注册一些与Linq2Sql相关的组件。我看到它们被创建,但它们在我的全局的Application_EndRequest方法被调用之前就被销毁了。这是设计出来的吗?有没有人知道这附近有什么工作?我想在我的UnitOfWork对象上调用commit,以便在每次请求结束时提交更改()。除了使用Global.asax Application_EndResult之外,我还尝试了IHttpModule,并获得了相同的结果。

我使用的是Castle 2.0。

下面是我如何向PerWebRequest注册我的东西。我正在创建一个持有L2S DataContext的DataCOntextProvider对象。该对象被注入到UoW中。

代码语言:javascript
复制
/// <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,如下所示:

代码语言:javascript
复制
 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();
        }

    }

谢谢,科里

EN

回答 2

Stack Overflow用户

发布于 2009-12-31 04:34:22

尝试将您的Application_EndRequest代码移动到一个before模块,并在PerWebRequestLifestyleModule之前注册它。

票数 1
EN

Stack Overflow用户

发布于 2010-07-28 13:55:36

您的IUnitOfWorkManager实现应该实现IDisposable,并在Dispose中调用SubmitChanges。或者,使用自定义退役提交更改关注。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1956530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档