I'm using ninject to manage my session for a Web API/MVC application. The code is as follows:
Bind<ISession>().ToMethod(c => c.Kernel.Get<ISessionFactory>().OpenSession())
.InRequestScope()
.OnActivation(s => s.BeginTransaction())
.OnDeactivation((s) =>
{
try
{
s.Transaction.Commit();
}
catch (Exception e)
{
s.Transaction.Rollback();
}
s.Close();
s.Dispose();
});
}
The OnActivation code is called correctly - when the session is injected a transaction is begun. However when the request finishes, the ondeactivation is not called. Therefore I can query things from the database but not commit changes (unless I commit the transaction elsewhere).
I'm not really sure why the OnDeactivation isn't being called - am I missing something in my ninject setup?