Hi everyone,
Is anyone aware of an issue trying to save an NHibernate session in
ASP.NET application using sqlserver as the session state mode? When
using inProc session state everything works fine, but when sql server
is used to manage state the application bombs out with the following
error:
"Unable to serialize the session state. In 'StateServer' and
'SQLServer' mode,
ASP.NET will serialize the session state objects,
and as a result non-serializable objects or MarshalByRef objects are
not permitted. The same restriction applies if similar serialization
is done by the custom session state store in 'Custom' mode."
Any ideas or thoughts would be really helpful. Thanks. Code below:
----------- Session Manager -------------
[Serializable]
public class StaticSessionManager
{
public static readonly ISessionFactory SessionFactory;
static StaticSessionManager()
{
try
{
var cfg = new NHibernate.Cfg.Configuration();
if (SessionFactory != null)
throw new Exception("trying to init SessionFactory
twice!");
SessionFactory = cfg.Configure().BuildSessionFactory
();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
throw new Exception("NHibernate initialization
failed", ex);
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
-------------------HttpModule----------------
public class NHibernateSessionPerConversationModule : IHttpModule
{
#region Constants
const string NHIBERNATE_CONVERSATION_END_FLAG_KEY =
"hibernate.conversation.end.flag";
const string NHIBERNATE_CURRENT_SESSION_KEY =
"hibernate.session";
#endregion
#region Public Methods
public void Dispose()
{
}
public static void SetEndConversationFlag()
{
HttpContext.Current.Items
[NHIBERNATE_CONVERSATION_END_FLAG_KEY] = true;
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler
(Application_BeginRequest);
context.PostRequestHandlerExecute += new EventHandler
(Application_EndRequest);
}
#endregion
#region Private Methods
private void Application_BeginRequest(object sender, EventArgs
e)
{
ISession currentSession;
if (HttpContext.Current.Session != null)
{
currentSession = (ISession)HttpContext.Current.Session
[NHIBERNATE_CURRENT_SESSION_KEY];
if (currentSession == null)
{
currentSession = StaticSessionManager.OpenSession
();
currentSession.FlushMode = FlushMode.Never;
}
}
else
{
currentSession = StaticSessionManager.OpenSession();
currentSession.FlushMode = FlushMode.Never;
}
CurrentSessionContext.Bind(currentSession);
currentSession.BeginTransaction();
}
private void Application_EndRequest(object sender, EventArgs
e)
{
ISession session = CurrentSessionContext.Unbind
(StaticSessionManager.SessionFactory);
if (HttpContext.Current.Items
[NHIBERNATE_CONVERSATION_END_FLAG_KEY] != null)
{
session.Flush();
session.Transaction.Commit();
session.Close();
HttpContext.Current.Session
[NHIBERNATE_CURRENT_SESSION_KEY] = null;
}
else
{
session.Transaction.Commit();
if (HttpContext.Current.Session != null)
{
HttpContext.Current.Session
[NHIBERNATE_CURRENT_SESSION_KEY] = session;
}
}
}