Question

I have a legacy WCF service using FluentNHibernate (along side EF and stored procs). The service was working fine for over an year. Recently we change the apppool recycle settings from default to make apppool recycle at 10 AM every day. Since that change, at least twice, my WCF service has failed with following exception

FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

 ---> FluentNHibernate.Cfg.FluentConfigurationException: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

 ---> NHibernate.MappingException: Could not configure datastore from input stream (XmlDocument) ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at NHibernate.Cfg.MappingsQueue.GetNextAvailableResource()
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
   --- End of inner exception stack trace ---
   at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
   at NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name)
   at NHibernate.Cfg.Configuration.AddDocument(XmlDocument doc, String name)
   at FluentNHibernate.PersistenceModel.Configure(Configuration cfg)
   at FluentNHibernate.Cfg.MappingConfiguration.Apply(Configuration cfg)
   at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
  --- End of inner exception stack trace ---
  at FluentNHibernate.Cfg.FluentConfiguration.BuildConfiguration()
  at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()
  --- End of inner exception stack trace ---
  at FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory()

A few interesting things to note

  1. Both times, the error has started appearing few seconds after the apppool recycle which makes me believe this is closely related to that
  2. This does not happen after every apppool recycle
  3. This does not happen in our test environments where apppool recycle setting is IIS default

This is very difficult to reproduce locally. I have looked at following SO questions regarding the same exceptions but they are in a completely different context and the authors of those questions are getting that exception consistently which is not the case here

Runtime error when trying to run Fluent NHibernate tutorial example

Fluent nHibernate error - Could not compile the mapping document: (XmlDocument)

Was it helpful?

Solution

After hours of investigation, it came down to be an issue of multiple threads trying to create session factory. I would have thought that NH session factory is thread-safe but sadly it isn't and I had to put int code to make it thread-safe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top