Servicestack NHibernate Auth Repo No CurrentSessionContext configured

115 views Asked by At

I have the following configuration:

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

And then elsewhere I have:

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);
    }
}

This all works for the rest of my app but when i try to use anything to do with the NH auth repo I get:

No CurrentSessionContext configured (set the property current_session_context_class)!

Anyone got any clues wha gwarn?

[UPDATE]

In my AppHost.Configure method I have added the following:

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

To no avail - I also have no idea how i would dispose of that :p

Also I am confused as ISessionFactory is injected in like every other part of my app:

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

So surely that should just work?

1

There are 1 answers

1
Ricardo Peres On

Yes, you need to explicitly bind the current session to the context:

CurrentSessionContext.Bind(session);

After you built the session. Don't forget to dispose of it too when you no longer need it.