Servicestack NHibernate Auth Repo没有配置CurrentSessionContext

时间:2014-12-09 09:13:05

标签: nhibernate fluent-nhibernate servicestack servicestack-auth

我有以下配置:

_container = new WindsorContainer ();
var factory = new SessionFactoryManager().CreateSessionFactory();
_container.Register(Component.For<NHibernate.ISessionFactory>().Instance(factory));

然后我在其他地方:

var authRepo = new NHibernateUserAuthRepository (_container.Resolve<NHibernate.ISessionFactory>());
_container.Register (Component.For<IAuthRepository>().Instance(authRepo));

public class SessionFactoryManager
{
    public ISessionFactory CreateSessionFactory()
    {
        try {
            var autoMap = AutoMap.AssemblyOf<Artist>()
                .Where(t => typeof(Entity).IsAssignableFrom(t))
                .UseOverridesFromAssemblyOf<LocationMappingOverride>();

            return Fluently.Configure()
                .Database(PostgreSQLConfiguration.PostgreSQL82.ConnectionString(c => c.FromConnectionStringWithKey("ConnectionString")).AdoNetBatchSize(50))
                .Mappings(m => m.AutoMappings.Add(autoMap))
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<ServiceStack.Authentication.NHibernate.UserAuthMap>())
                .ExposeConfiguration(TreatConfiguration)
                .BuildSessionFactory();
        } catch (Exception ex) {
            var m = ex;
        }

        return null;
    }

    protected virtual void TreatConfiguration(NHibernate.Cfg.Configuration configuration)
    {
        configuration.SetProperty("generate_statistics", "true");
        configuration.SetProperty("current_session_context_class", "thread");
        var update = new SchemaUpdate(configuration);
        update.Execute(false, true);
    }
}

这一切都适用于我的应用程序的其余部分但是当我尝试使用任何与NH auth repo做的事情时,我得到:

No CurrentSessionContext configured (set the property current_session_context_class)!

任何人都有任何线索吗?

[UPDATE]

在我的AppHost.Configure方法中,我添加了以下内容:

this.GlobalRequestFilters.Add ((req, res, vm) => {
    CurrentSessionContext.Bind(container.Resolve<NHibernate.ISession>());
});

无济于事 - 我也不知道如何处理它:p

另外我很困惑,因为ISessionFactory像我的应用程序的其他部分一样被注入:

https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Authentication.NHibernate/NHibernateUserAuthRepository.cs#L20

当然,只是工作吗?

1 个答案:

答案 0 :(得分:1)

是的,您需要将当前会话显式绑定到上下文:

CurrentSessionContext.Bind(session);

构建会话后。当你不再需要它时,不要忘记处理它。