For an application I am working on, we've started to get the following exception from NHibernate in Visual Studio 2014 on an oddly-specific basis:
Could not load file or assembly 'FluentNHibernate, Version=1.2.0.712, Culture=neutral, PublicKeyToken=8aa435e3cb308880' or one of its dependencies. An API call exited abnormally. (Exception from HRESULT: 0x800300FA (STG_E_ABNORMALAPIEXIT))
When we make server code changes, which causes the application to re-compile, the app works without any issues. This is only occurring if we attempt to run our Web project without the project rebuilding first.
We have manually ensured that the DLL is getting deployed to the bin
folder. We have ensured that the application has a reference to FluentNHibernate v1.2.0.712
. We know this is the case, because the first run is functioning as expected.
The code where this is occurring looks like this:
public class DataContext : IDisposable
{
private static ISessionFactory _sessionFactory;
private static bool _startupComplete;
private static object _locker;
public void Dispose()
{
_locker = null;
_sessionFactory = null;
}
public static ISession GetSession()
{
EnsureStartup();
ISession session = _sessionFactory.OpenSession();
session.BeginTransaction();
return session;
}
public static void EnsureStartup()
{
if(_startupComplete)
return;
lock(_locker)
{
if(! _startupComplete)
{
bool success = PerformStartup();
_startupComplete = success;
}
}
}
public static bool PerformStartup()
{
return InitializeSessionFactory();
}
public static bool InitializeSessionFactory()
{
try
{
Configuration configuration = BuildConfiguration();
_sessionFactory = configuration.BuildSessionFactory();
return true;
}
catch(Exception ex)
{
return string.IsNullOrEmpty(ex.Message);
}
}
public static Configuration BuildConfiguration()
{
Configuration configuration = Fluently.Configure(
new Configuration().Configure())
// Mappings uses reflection to add mappings from the specified assembly
.Mappings(cfg => cfg.FluentMappings.AddFromAssembly(typeof(DataContext).Assembly))
.BuildConfiguration();
return configuration;
}
class IncludeCastleDllInBuild : NHibernate.ByteCode.Castle.ProxyFactory
{
// Note: prevents missing DLL issue with the Castle DLL
}
}
We first detected it due to the ISession
assignment in GetSession
getting a Null Reference exception. Upon drilling deeper and F11/F10 stepping through our code, we discovered that they try/catch
in InitializeSessionFactory
was swallowing errors. I modified the code to force both PerformStartup
and InitializeSessionFactory
to correctly report whether or not the factory setup was successful.
Question: Why is NHibernate failing to load up, but only on subsequent runs of our application? More importantly, how do we get rid of this irritating exception?
EDIT 1: Per @DaveZych, I added an IDisposable
implementation to the class that clears out the _locker
and _sessionFactory
fields, but it had no effect on the problem. Additionally, in our web.config
files, we have no line turning Impersonation on, and based on the impersonation documentation, Impersonation is off by default.
EDIT 2: For grins and giggles, I added the following code to web.config
, inside the system.web
section:
<authentication mode="Windows" />
<identity impersonate="false" />
Unfortunately, this had no effect either, despite an earlier question about the same generic error. It should also be noted I am using the latest version of Visual Studio 2013.
User contributions licensed under CC BY-SA 3.0