温莎城堡 PerWebRequest 生活方式和 Application_EndRequest

发布于 2024-08-16 04:07:30 字数 1874 浏览 6 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞花火° 2024-08-23 04:07:30

尝试将您的 Application_EndRequest 代码移至 httpmodule 并
PerWebRequestLifestyleModule 之前注册它。

Try moving your Application_EndRequest code to a httpmodule and
register it before the PerWebRequestLifestyleModule.

回忆凄美了谁 2024-08-23 04:07:30

您的 IUnitOfWorkManager 实现应该实现 IDisposable 并在 Dispose 中调用 SubmitChanges。或者使用自定义停用提交更改问题。

your implementation of IUnitOfWorkManager should implement IDisposable and in Dispose call SubmitChanges. Alternatively use custom decommission submit changes concern.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文