0

是的。这是一个与我如何要求 Castle Windsor 解决我的 ISession 有关的问题,但我已经重读了 5 次代码,但仍然无法弄清楚它有什么问题。

下面是我相当标准的 NHibernate 设施,除了我注册的最后两个组件:

 public class NHibernateFacility : AbstractFacility
    {
        protected override void Init()
        {
            var config = new Configuration().Configure();
            Kernel.Register(
                Component.For<ISessionFactory>()
                    .UsingFactoryMethod((kernel, context) => config.BuildSessionFactory()),
                Component.For<ISession>()
                    .UsingFactoryMethod((kernel, context) => kernel.Resolve<ISessionFactory>().OpenSession())
                    .LifestylePerWebRequest(),
                     Component.For<IStatelessSession>()
                    .UsingFactoryMethod((kernel, context) => kernel.Resolve<ISessionFactory>().OpenStatelessSession())
                    .LifestylePerWebRequest(),
                    Component.For<StatefulSessionWrapper>().LifestylePerWebRequest(),
                    Component.For<StatelessSessionWrapper>().LifestylePerWebRequest()
                );
        }
    }

StatefulSessionWrapper/StatelessSessionWrapper 用作 ISession/IStateless 会话的...包装器,如下所示:

public class StatefulSessionWrapper : ISessionWrapper
    {
        public StatefulSessionWrapper(ISession session)
        {
            this.Session = session;
        }

        private readonly ISession Session;

        #region ISessionWrapper Members

        public ITransaction BeginTransaction()
        {
            return Session.BeginTransaction();
        }

        public ITransaction BeginTransaction(IsolationLevel isolationLevel)
        {
            return Session.BeginTransaction(isolationLevel);
        }

        public ITransaction Transaction { get { return Session.Transaction; } }

        public bool IsConnected
        {
            get { return Session.IsConnected; }
        }

        public bool IsOpen
        {
            get { return Session.IsOpen; }
        }

        #endregion
    }

这样,我可以使用单个操作过滤器,它只处理 NHibernate 会话的事务部分:

public class NHibernateActionFilter<T> : Castle.MonoRail.Framework.IFilter where T:ISessionWrapper
    {
        private readonly T NHibernateSession;

        public NHibernateActionFilter(T session)
        {
            if (session != null)
                NHibernateSession = session;
            else
                throw new NullReferenceException("Session is null");
        }

        #region IFilter Members

        public bool Perform(ExecuteWhen exec, IEngineContext context, IController controller, IControllerContext controllerContext)
        {
            if (exec == ExecuteWhen.BeforeAction)
            {
                NHibernateSession.BeginTransaction();
                return true;
            }
            if (exec == ExecuteWhen.AfterAction)
            {
                if (NHibernateSession.Transaction == null || !NHibernateSession.Transaction.IsActive)
                    return false;
                if (context.LastException != null)
                {
                    NHibernateSession.Transaction.Rollback();
                    return false;
                }
                else
                {
                    NHibernateSession.Transaction.Commit();
                    return true;
                }

            }
            return false;
        }

        #endregion
    }

并像这样简单地使用它:

    [Filter(ExecuteWhen.Always, typeof(NHibernateActionFilter<StatefulSessionWrapper>), ExecutionOrder = 1)]
    [Filter(ExecuteWhen.BeforeAction, typeof(AuthenticationFilter), ExecutionOrder = 2)]
    [Layout("Default"), Rescue("Default")]
    public abstract class NHibernateController : SmartDispatcherController
    {
        public NHibernateController(ISession session)
        {
            this.NHibernateSession = session;
        }

        protected readonly ISession NHibernateSession;
        ..................

唯一的问题?

这是救援页面。请参阅下面的异常详细信息

ObjectDisposedException

消息:会话已关闭!对象名称:'ISession'。

StackTrace:在 NHibernate.Impl.AbstractSessionImpl.ErrorIfClosed() 在 p:\nhibernate-core\src\NHibernate\Impl\AbstractSessionImpl.cs:第 207 行在 NHibernate.Impl.AbstractSessionImpl.CheckAndUpdateSessionStatus() 在 p:\nhibernate-core\ src\NHibernate\Impl\AbstractSessionImpl.cs:NHibernate.Impl.SessionImpl.BeginTransaction() 的第 199 行,p:\nhibernate-core\src\NHibernate\Impl\SessionImpl.cs:ADAutoTotal.Monorail.StatefulSessionWrapper.BeginTransaction 的第 1456 行() 在 C:\Dropbox\Projects\ADAutoTotal\ADAutoTotal.Monorail\StatefulSessionWrapper.cs:第 23 行 ADAutoTotal.Monorail.NHibernateActionFilter`1.Perform(ExecuteWhen exec, IEngineContext context, IController controller, IControllerContext controllerContext) in C:\Dropbox \Projects\ADAutoTotal\ADAutoTotal.Monorail\NHibernateActionFilter.cs:C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework 中 Castle.MonoRail.Framework.Controller.ProcessFilter(ExecuteWhen when, FilterDescriptor desc) 的第 31 行\Controller.cs:C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework.Controller.ProcessFilters(IExecutableAction action, ExecuteWhen when) 的第 2099 行Castle.MonoRail.Framework\Controller.cs:C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac 中 Castle.MonoRail.Framework.Controller.RunBeforeActionFilters(IExecutableAction action, Boolean& cancel) 的第 2054 行\MR2\src\Castle.MonoRail.Framework\Controller.cs:Castle.MonoRail.Framework.Controller 的 1953 行。RunActionAndRenderView() 在 C:\Dropbox\Projects\Monorail\castleproject-MonoRail-98c93ac\castleproject-MonoRail-98c93ac\MR2\src\Castle.MonoRail.Framework\Controller.cs:line 1622

因此,很明显 NHibernateActionFilter 获得了一个封闭的会话。但在我看来,不应该。哈尔普。

4

1 回答 1

1

好吧,事实证明我忘了在我的动作过滤器中添加他们正确的生活方式,因此让温莎让他们成为单身......

于 2013-01-18T08:50:59.387 回答