4

我正在使用 Castle Windors,它是AutoTx和 haf 的NHibernate Facility。最终,我想要 AutoTx 提供的 Transaction 属性易于使用的好处。(ASP.NET MVC 4 项目)。

我正在使用 Castle.Facilities.NHibernate.ISessionManager 来管理我的 PerWebRequest 会话。我已经这样设置了 Windsor 安装程序:

public void Install(Castle.Windsor.IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {
            container.AddFacility<AutoTxFacility>();
            container.Register(Component.For<INHibernateInstaller>().ImplementedBy<NHibernateInstaller>().LifeStyle.Singleton);            
            container.AddFacility<NHibernateFacility>(f => f.DefaultLifeStyle = DefaultSessionLifeStyleOption.SessionPerWebRequest);

            container.Install(FromAssembly.Containing<PersonRepository>());
        }

我正在使用 SessionPerWebRequest 的 DefaultLifeStyle,我希望它会做到这一点,为我提供一个持续整个 Web 请求的会话,这样在同一请求中对 SessionManager 上的所有 OpenSession 调用都使用相同的会话。我正在使用以下代码对其进行测试:

public class HomeController : Controller
{        
    private readonly ISessionManager _sessionManager;

    public HomeController(ISessionManager sessionManager)
    {            
        _sessionManager = sessionManager;
    }

    public ActionResult Index()
    {
        using (var session1 = _sessionManager.OpenSession())
        {
            var person = session1.Get<Person>(1);
            using (var session2 = _sessionManager.OpenSession())
            {
                var person2 = session2.Get<Person>(1);
            }
        }

        return View();
    }        
}

并检查日志以查看每个已创建会话的 ID。id 总是不同的。例如

05/01/2013 11:27:39.109 DEBUG 9 NHibernate.Impl.SessionImpl - [session-id=c1ba248a-14ba-4468-a20c-d6114b7dac61] opened session at timestamp: 634929820591, for session factory: [/ea869bb12b4d4e51b9f431a4f9c9d9fa]
05/01/2013 11:30:36.383 DEBUG 9 NHibernate.Impl.SessionImpl - [session-id=72481180-625d-4085-98e9-929e3fd93e8a] opened session at timestamp: 634929822363, for session factory: [/ea869bb12b4d4e51b9f431a4f9c9d9fa]

值得注意的是,我并没有以 Handlers 的方式在 web.config 中添加任何内容。我需要吗?(我在 NHib Facility Wiki 中没有看到任何文件表明这一点)我是否期望相同的 Session 总是返回不正确。

我查看了该设施的源代码,但不了解每个 Web 请求的会话是如何被实例化的,以及对 OpenSession 的多次调用将如何在同一个 Web 请求中产生同一个会话。

以下是 SessionManager 向 Windsor 注册的方式:

Component.For<ISessionManager>().Instance(new SessionManager(() =>
                    {
                        var factory = Kernel.Resolve<ISessionFactory>(x.Instance.SessionFactoryKey);
                        var s = x.Instance.Interceptor.Do(y => factory.OpenSession(y)).OrDefault(factory.OpenSession());
                        s.FlushMode = flushMode;
                        return s;
                    }))
                        .Named(x.Instance.SessionFactoryKey + SessionManagerSuffix)
                        .LifeStyle.Singleton

ISession 使用以下方法在 Windsor 中注册

private IRegistration RegisterSession(Data x, uint index)
{
    Contract.Requires(index < 3,
                      "there are only three supported lifestyles; per transaction, per web request and transient");
    Contract.Requires(x != null);
    Contract.Ensures(Contract.Result<IRegistration>() != null);

    return GetLifeStyle(
        Component.For<ISession>()
            .UsingFactoryMethod((k, c) =>
            {
                var factory = k.Resolve<ISessionFactory>(x.Instance.SessionFactoryKey);
                var s = x.Instance.Interceptor.Do(y => factory.OpenSession(y)).OrDefault(factory.OpenSession());
                s.FlushMode = flushMode;
                logger.DebugFormat("resolved session component named '{0}'", c.Handler.ComponentModel.Name);
                return s;
            }), index, x.Instance.SessionFactoryKey);
}

private ComponentRegistration<T> GetLifeStyle<T>(ComponentRegistration<T> registration, uint index, string baseName)
            where T : class
{
    Contract.Requires(index < 3,
                      "there are only three supported lifestyles; per transaction, per web request and transient");
    Contract.Ensures(Contract.Result<ComponentRegistration<T>>() != null);

    switch (defaultLifeStyle)
    {
        case DefaultSessionLifeStyleOption.SessionPerTransaction:
            if (index == 0)
                return registration.Named(baseName + SessionPerTxSuffix).LifeStyle.PerTopTransaction();
            if (index == 1)
                return registration.Named(baseName + SessionPWRSuffix).LifeStyle.PerWebRequest;
            if (index == 2)
                return registration.Named(baseName + SessionTransientSuffix).LifeStyle.Transient;
            goto default;
        case DefaultSessionLifeStyleOption.SessionPerWebRequest:
            if (index == 0)
                return registration.Named(baseName + SessionPWRSuffix).LifeStyle.PerWebRequest;
            if (index == 1)
                return registration.Named(baseName + SessionPerTxSuffix).LifeStyle.PerTopTransaction();
            if (index == 2)
                return registration.Named(baseName + SessionTransientSuffix).LifeStyle.Transient;
            goto default;
        case DefaultSessionLifeStyleOption.SessionTransient:
            if (index == 0)
                return registration.Named(baseName + SessionTransientSuffix).LifeStyle.Transient;
            if (index == 1)
                return registration.Named(baseName + SessionPerTxSuffix).LifeStyle.PerTopTransaction();
            if (index == 2)
                return registration.Named(baseName + SessionPWRSuffix).LifeStyle.PerWebRequest;
            goto default;
        default:
            throw new FacilityException("invalid index passed to GetLifeStyle<T> - please file a bug report");
    }
}

哪个确实将 ISession 注册为 PerWebRequest,但是我在代码中看不到在需要会话时提取该命名注册的任何地方?

任何有关我需要做什么的帮助,以使每个 Web 请求的 Session 正常工作,我们都将不胜感激。

更新 我决定将传递给 SessionManager 构造函数的代码函数替换为从容器中获取 ISession 的代码,而不是使用工厂。非常适合我想要它做的事情,包括被包裹在事务中,每个 Web 请求只打开一个会话,或者瞬态等。

Component.For<ISessionManager>().Instance(new SessionManager(() =>
{
    var s = Kernel.Resolve<ISession>();
    s.FlushMode = flushMode;
    return s;
}))
//Component.For<ISessionManager>().Instance(new SessionManager(() =>
//{
//    var factory = Kernel.Resolve<ISessionFactory>(x.Instance.SessionFactoryKey);
//    var s = x.Instance.Interceptor.Do(y => factory.OpenSession(y)).OrDefault(factory.OpenSession());
//    s.FlushMode = flushMode;
//    return s;
//}))
    .Named(x.Instance.SessionFactoryKey + SessionManagerSuffix)
    .LifeStyle.Singleton

我希望 Kernel.Resolve() 会抓取容器中的第一个注册服务。这将是我将生活方式设置为的任何内容。

4

0 回答 0