Question

In my S#arp Arch 2.0 project, I'm communicating with 2 databases. This runs fine locally with the ASP.Net Development Server (VS 2010) and passes unit tests requiring talking to either database.

Next step was to Publish the project (using VS' built-in "Publish" menu option) to the in-house development server (Windows Server 2008 R2) and fire this thing up on a real server where people could actually see it.

Now I get the exception shown in the title when I try to run the application. The exception is thrown at the = new NHibernateConfigurationFileCache() line below:

private void InitialiseNHibernateSessions()
    {
        NHibernateSession.ConfigurationCache = new NHibernateConfigurationFileCache();

        NHibernateSession.InitStorage(this.webSessionStorage);

        NHibernateSession.AddConfiguration(NHibernateSession.DefaultFactoryKey,
            new[] { Server.MapPath("~/bin/SRN2.Infrastructure.dll") },
            new AutoPersistenceModelGenerator().Generate(),
            Server.MapPath("~/NHibernate.config"),
            null, null, null);


        NHibernateSession.AddConfiguration(SRN2.Infrastructure.DataGlobals.OTHER_DB_FACTORY_KEY,
            new string[] { Server.MapPath("~/bin/SRN2.Infrastructure.dll") },
            new AutoPersistenceModelGenerator().Generate(),
            Server.MapPath("~/NHibernate-OTHER.config"),
            null, null, null);

    }

Stack trace:

[InvalidOperationException: Cannot set the ConfigurationCache property after calling Init]
   SharpArch.NHibernate.NHibernateSession.set_ConfigurationCache(INHibernateConfigurationCache value) +105
   SRN2.Web.Mvc.MvcApplication.InitialiseNHibernateSessions() in C:\code\SRN2-Sharp2\trunk\Solutions\SRN2.Web.Mvc\Global.asax.cs:122
   SharpArch.NHibernate.NHibernateInitializer.InitializeNHibernateOnce(Action initMethod) +116
   SRN2.Web.Mvc.MvcApplication.Application_BeginRequest(Object sender, EventArgs e) in     C:\code\SRN2-Sharp2\trunk\Solutions\SRN2.Web.Mvc\Global.asax.cs:71
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +148
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75
Was it helpful?

Solution

Jon is right, it does sound like your InitialiseNHibernateSessions method is being called multiple times. You don't have to use the config cache, have you tried disabling it?

The NHibernate configuration is cached to file in order to improve start up time. If the configuration has not changed it is loaded from the cache file. The default location of the cache file is the system temporary file folder (e.g. Path.GetTempPath()).

If you don't have file permissions, or don't need config caching, just remove or comment out the line that initialises the configuration cache, i.e. this line:

NHibernateSession.ConfigurationCache = new NHibernateConfigurationFileCache();

OTHER TIPS

This error occurred, for one application, roughly every month or two months for an extended period. I haven't really been able to fix this permanently but I did discover that the following procedure resolves the error for:

  1. Delete the temporary configuration cache files; they should be located in the Windows temporary files folder and there should be a file for each database (e.g. DatabaseName--1973822310.bin) and another named something like nhibernate.current_session--1973822310.bin.
  2. Restart the IIS web site for your application.
  3. Recycle the application pool for your application.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top