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
<[email protected]> wrote:
> 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
--
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en.