ASP.NET application problem - can't configure NHibernate

981 views
Skip to first unread message

Sergey Lobko-Lobanovsky

unread,
Nov 2, 2010, 1:03:40 PM11/2/10
to nhu...@googlegroups.com
Greetings NHibernate folks,

I've been banging my head against the wall half of the day today to solve a mysterious problem. Today I started implementing an ASP.NET MVC 2 front-end for my application. I didn't use HN before, but I already have a bunch of unit tests running just fine. 

In Global.asax.cs I have the following problem. As soon as I call "new Configuration()" in Application_BeginRequest, a NotSupportedException is thrown from deep inside of log4net. Apparently, it tries to get the Location attribute of a dynamically generated assembly, and fails.

NH is configured in web.config as follows:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="KupiKupon.Core">
<property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
<property name="connection.connection_string">
Server=127.0.0.1;Port=5432;User Id=kupikupon;Password=kupikupon;Database=kupikupon_test;
</property>
<property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
<property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
</session-factory>
</hibernate-configuration>

Notes:
  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:

[NotSupportedException: The invoked member is not supported in a dynamic assembly.]
   System.Reflection.Emit.AssemblyBuilder.get_Location() +68
   log4net.Util.SystemInfo.AssemblyLocationInfo(Assembly myAssembly) +92
   log4net.Core.DefaultRepositorySelector.GetInfoForAssembly(Assembly assembly, String& repositoryName, Type& repositoryType) +137
   log4net.Core.DefaultRepositorySelector.CreateRepository(Assembly repositoryAssembly, Type repositoryType, String repositoryName, Boolean readAssemblyAttributes) +224
   log4net.Core.DefaultRepositorySelector.GetRepository(Assembly repositoryAssembly) +38
   log4net.Core.LoggerManager.GetLogger(Assembly repositoryAssembly, String name) +57
   log4net.LogManager.GetLogger(Type type) +40
   lambda_method(ExecutionScope , Type ) +12
   NHibernate.Log4NetLoggerFactory.LoggerFor(Type type) in d:\CSharp\NH\nhibernate\src\NHibernate\Logging.cs:238
   NHibernate.LoggerProvider.LoggerFor(Type type) in d:\CSharp\NH\nhibernate\src\NHibernate\Logging.cs:119
   NHibernate.Cfg.Configuration..cctor() in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:79

[TypeInitializationException: The type initializer for 'NHibernate.Cfg.Configuration' threw an exception.]
   NHibernate.Cfg.Configuration..ctor() in d:\CSharp\NH\nhibernate\src\NHibernate\Cfg\Configuration.cs:262
   KupiKupon.Web.MvcApplication.PrepareDatabase() in C:\_Maygem\Projects\KupiKupon\kupikupon-server\KupiKupon\KupiKupon.Web\Global.asax.cs:38
   KupiKupon.Core.KKApplication.PrepareContainer() in C:\_Maygem\Projects\KupiKupon\kupikupon-server\KupiKupon\KupiKupon.Core\KKApplication.cs:48
   KupiKupon.Web.MvcApplication.Application_BeginRequest(Object sender, EventArgs e) in C:\_Maygem\Projects\KupiKupon\kupikupon-server\KupiKupon\KupiKupon.Web\Global.asax.cs:65
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +80
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +171

Jason Meckley

unread,
Nov 2, 2010, 8:51:53 PM11/2/10
to nhusers
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:*

Sergey Lobko-Lobanovsky

unread,
Nov 2, 2010, 6:48:38 PM11/2/10
to nhu...@googlegroups.com
I sorta resolved this by disabling the Log4Net logging in web.config.

Note: I am using NHibernate 3.0 Beta1

Sergey Lobko-Lobanovsky

unread,
Nov 3, 2010, 4:30:57 AM11/3/10
to nhu...@googlegroups.com
Thanks Jason, your help is appreciated! 
Do you know what the reason is behind not calling new Configuration() in BeginRequest? It's that using static members in a web application is something I consider "smell" :)

Sergey.

--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.


kor

unread,
Nov 3, 2010, 5:16:20 AM11/3/10
to nhusers
building a configuration is an expansive operation that it's needed
only once for application life, if you want you can inject it with a
ioc and singletone behaviour or you can move it to an external static
class

Sergey Lobko-Lobanovsky

unread,
Nov 3, 2010, 7:55:15 AM11/3/10
to nhu...@googlegroups.com
Understood, thanks. Actually I am already using an IoC container called Autofac.


--

Jason Meckley

unread,
Nov 3, 2010, 8:03:29 AM11/3/10
to nhusers
register the session factory with autofac as a singleton.

On Nov 3, 7:55 am, Sergey Lobko-Lobanovsky
<serge.lobanov...@gmail.com> wrote:
> Understood, thanks. Actually I am already using an IoC container called
> Autofac.
>
> On Wed, Nov 3, 2010 at 11:16 AM, kor <korkl...@yahoo.it> wrote:
> > building a configuration is an expansive operation that it's needed
> > only once for application life, if you want you can inject it with a
> > ioc and singletone behaviour or you can move it to an external static
> > class
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to nhu...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
> > .

Sergey Lobko-Lobanovsky

unread,
Nov 5, 2010, 7:11:49 AM11/5/10
to nhu...@googlegroups.com
Thanks everyone for suggestions, I resolved this by using an HttpModule.

To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages