Asp.net 温莎城堡PerWebRequest生活方式和应用程序

Asp.net 温莎城堡PerWebRequest生活方式和应用程序,asp.net,castle-windsor,global-asax,perwebrequest,Asp.net,Castle Windsor,Global Asax,Perwebrequest,我正在使用PerWebRequest生活方式注册一些与Linq2Sql相关的组件。我看到它们被创建了,但在调用我的全局应用程序的EndRequest方法之前,它们被销毁了。这是故意的吗?有人知道附近的工作吗?我想在每个请求结束时调用UnitOfWork对象上的commit以提交更改()。除了使用Global.asax应用程序_EndResult外,我还尝试了一个具有相同结果的IHttpModule 我正在使用Castle 2.0 以下是我如何向PerWebRequest注册我的资料。我正在创建一

我正在使用PerWebRequest生活方式注册一些与Linq2Sql相关的组件。我看到它们被创建了,但在调用我的全局应用程序的EndRequest方法之前,它们被销毁了。这是故意的吗?有人知道附近的工作吗?我想在每个请求结束时调用UnitOfWork对象上的commit以提交更改()。除了使用Global.asax应用程序_EndResult外,我还尝试了一个具有相同结果的IHttpModule

我正在使用Castle 2.0

以下是我如何向PerWebRequest注册我的资料。我正在创建一个DataCOntextProvider对象,该对象保存在L2S DataContext上。该对象被注入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>());
    }
//
///注册IUnitOfWorkManager,以便根据web请求解析为LinqToSqlUnitOfWorkManager
/// 
公共无效注册表linq1sqlunitofworkperwebrequest()
{
_container.Register(Component.For())
.livelope.PerWebRequest
.ImplementedBy());
}
/// 
///注册IDataContextProvider以根据web请求解析为DataContextProvider
/// 
公共无效注册表DataContextProviderPerWebRequest()
{
_container.Register(Component.For())
.livelope.PerWebRequest
.ImplementedBy());
}
现在,我只是尝试通过CommonServiceLocator(CSL和Windsor适配器都是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();
        }

    }
受保护的无效应用程序\u EndRequest(对象发送方,事件参数e)
{
//忽略,除非这是页面(.aspx)或处理程序(.ashx)
如果(!RequestCanHaveContext())
返回;
//去找工作经理
var uow=ServiceLocator.Current.GetInstance();
//如果有,请在请求结束时提交更改
如果(uow!=null)
{
//不要显式地处理uow,否则我们将在上下文中处理异常
提交();
}
}
谢谢,
Corey

尝试将
应用程序的EndRequest
代码移动到httpmodule,然后
PerWebRequestLifestyleModule
之前注册它

您的
IUnitOfWorkManager
的实现应该实现
IDisposable
和in-Dispose调用提交更改。或者使用自定义停用提交更改。

您是否验证了模块的endrequest处理程序在组件销毁之前运行?另外,您在哪里/如何处理内核的销毁事件?我做到了。我的行为非常古怪——在不触发微内核的销毁事件的情况下创建/销毁东西。我不知道是否是ASP.NET导致了我的痛苦。您是否在组件销毁之前验证了模块的endrequest处理程序运行?另外,您在哪里/如何处理内核的销毁事件?