Monday, October 24, 2011

NHibernate and Missing Castle dll

The Problem

MSbuild does not see a reference to the NHibernate.ByteCode.Castle assembly when building a project. Thus,  it does not copy the file into the output directory. This causes an exception when trying to execute code which relies on NHibernate.

The exception seen is usually something like: Could not load file or assembly 'NHibernate.ByteCode.Castle' or one of its dependencies...

The Solution

Bury a reference to the Castle assembly within your code:

class IncludeCastleDllInBuild : NHibernate.ByteCode.Castle.ProxyFactory
    //note : prevents missing dll issue with the Castle dll