the first problem is that you should not call new Configuration in
begin request. in it's simplest form the global asax can look like
this
class Global : HttpApplicaiton
{
public static ISessionFactory factory {get; private set;}
public Global()
{
BeginRequest += (o, e) =>
CurrentSessionContext.Bind(factory.OpenSession());
EndRequest += (o, e) =>
CurrentSessionContext.Unbind(factory).Dispose();
}
protected void Application_Start(object sender, EventArgs e)
{
factory = new
Configuration().Configure().BuildSessionFactory();
}
protected void Application_End(object sender, EventArgs e)
{
factory.Dispose();
}
}
create an action filter to manage the transaction
class TransactionFilter : Filter
{
public bool BeforeAction(Context context...)
{
Global.factory.GetCurrentSession().BeginTransaction();
}
public void AfterAction(Context context...)
{
using(var tx = Global.factory.GetCurrentSession()Transaction)
{
if(context.LastError == null)
{
tx.Commit();
}
else
{
tx.Rollback();
}
}
}
}
then your controllers can look like this
class MyController : Controller
{
[TransactionFilter]
public ActionResult Index(long id);
{
var data = Global.factory.GetCurrentSession().Get<Entity>(id);
return ViewResult{Model = data};
}
}
if you're using an IoC container than you can remove of a public
static property on global for the factory. The key is that Session
Factory is a singleton. from this point NH has exhaustive error
checking to ensure the configuration and mappings are correct. follow
the advice of the exception message and you should be able to solve
most problems.
On Nov 2, 1:03 pm, Sergey Lobko-Lobanovsky
> 1. Yes, initially I tried to configure NH from inside the
> Application_Start method, but it fails there as well.
> 2. Yes, I have tried using hibernate.cfg.xml instead of web.config with
> no avail.
> 3. Yes, everything else works fine as I have several unit tests covering
> the basic repositories (DAO classes), and they work just fine. The
> configuration code is similar both in the web app and in the unit test
> project.
>
> I just don't know what else to try. The entire exception & stack trace are
> below.
>
> Any help will be much appreciated!
>
> Sergey.
>
> {Line 38: NHibernate.Cfg.Configuration config = new
> NHibernate.Cfg.Configuration().Configure();Line 39:
> config.AddAssembly(typeof(CouponSale).Assembly);
> Line 40: return config;
>
> * Source File: *C:\_Maygem\Projects\KupiKupon\kupikupon-server\KupiKupon\KupiKupon.Web\Global.asax.cs
> * Line: * 38
>
> *Stack Trace:*