-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How to configure NHibernate with ASP.NET
PostPosted: Thu Dec 23, 2010 5:32 am 
Newbie

Joined: Mon Dec 20, 2010 12:10 am
Posts: 1
Hi All,

Any help would be much appreciated. It took long time to solve this issue. But I haven't got the solution.
In my ASP.NET web application,I used NHibernate.dll and configuration is read from the Web.config file as follows.
At Web.config
<httpModules>
<add name="CurrentSessionModule" type="Globals.CurrentSessionModule, Globals"/>
</httpModules>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.connection_string">Server=localhost;initial catalog=TESTDB;Integrated Security=SSPI</property>
<property name="current_session_context_class">web</property>
<property name="default_schema">dbo</property>
<mapping assembly="LAT.Domain"/>
</session-factory>
</hibernate-configuration>

At CurrentSessionModule.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using DataAccess;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Context;

namespace Globals
{
public class CurrentSessionModule : IHttpModule
{


public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.BeginRequest += delegate
{
string session = Global.SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
};
context.EndRequest += delegate
{
CurrentSessionContext.Unbind(Global.SessionFactory);

};
}

I reference this website( http://dotnetslackers.com/articles/aspn ... P-NET.aspx)
but the main different between my project and the ref website is Global.asax
my Global.asax

<%@ Import namespace="System.Collections.Generic"%>
<%@ Import namespace="System.Xml"%>
<%@ Import namespace="System.IO"%>
<%@ Import namespace="System.Web.Configuration"%>
<%@ Application Language="C#" Inherits="Globals.LATApplication"%>

<script runat="server">


private static void LogData(string url, string httpSessionId, string Id)
{
if (!WebConfigurationManager.AppSettings["debugMode"].ToLower().Equals("none"))
try{
string fname = WebConfigurationManager.AppSettings["debugSessionLogFile"];
XmlDocument doc = new XmlDocument();
if (File.Exists(fname))
doc.Load(fname);
else
doc.LoadXml("<SessionLog><Statistics HttpSessions=\"0\" NHibernateSessions=\"0\"/><Entries/></SessionLog>");
XmlElement entry = doc.CreateElement("Entry");
doc.DocumentElement.GetElementsByTagName("Entries")[0].AppendChild(entry);
XmlAttribute att = doc.CreateAttribute("Time");
att.Value = DateTime.Now.ToString();
entry.Attributes.Append(att);
att = doc.CreateAttribute("Url");
att.Value = url;
entry.Attributes.Append(att);
XmlElement ele = doc.CreateElement("HttpSession");
ele.InnerText = httpSessionId;
entry.AppendChild(ele);
ele = doc.CreateElement("NHibernateSession");
ele.InnerText = Id;
entry.AppendChild(ele);
doc.Save(fname);
}
catch{}
}


void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
if (!WebConfigurationManager.AppSettings["debugMode"].ToLower().Equals("none"))
{
string fname = WebConfigurationManager.AppSettings["debugSessionLogFile"];
try
{
if (File.Exists(fname))
File.Delete(fname);
}
catch { }
LogData("*Application Starting*", "*", "*");
}


}

void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
LogData("*Application Ending*", "*", "*");
}

void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
try
{
Exception ex = HttpContext.Current.Server.GetLastError();
string err = ex.Message;
string ierr = "";
if (ex.InnerException != null)
ierr = ex.InnerException.Message;
LogData("*Application Error*", "*" + err + "*", "*" + ierr + "*");
}
catch (Exception except)
{
LogData("*Application Error*", "*" + except.Message + "*", "*Application_Error Error*");
}
}

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
LogData("*Session Starting*", Session.SessionID, "*");
}

void Session_End(object sender, EventArgs e)
{
LogData("*Session Ending*", Session.SessionID, "*");
}

</script>


When I run the web application, I got the error " Illeglly attempt to associate a proxy with two open session".
I am not really sure how that would happen. I try some solution but haven't solved the issues.

Can anyone help me with this?

Regards
Su Hlaing


Top
 Profile  
 
 Post subject: Re: How to configure NHibernate with ASP.NET
PostPosted: Thu Dec 23, 2010 5:51 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
sorry, this forum is for Java Hibernate only, I have no clue about .Net

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.