Upgrading To NHibernate 2.1

20 commentsWritten on March 15th, 2009 by
Categories: NHibernate

This is something that a few people (those who don't read release notes ;) ) are going to run into, so i figured i'd post the solution here.

If your application uses NHibernate 2.0, and you want to upgrade to NHibernate 2.1 you need to add something to your NHibernate configuration. If you just replace NHibernate.dll with the newer version and start your application, you will get an exception with the following message:

The ProxyFactoryFactory was not configured. Initialize 'proxyfactory.factory_class' property of the session-factory configuration section with one of the available NHibernate.ByteCode providers. Example: <property name='proxyfactory.factory_class'>NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> Example: <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

Pretty helpful message, no? (if only everyone would actually read the content of exception messages)

NHibernate used to default to use Castle's DynamicProxy to generate runtime proxies, but you now have to explicitly state which proxy factory should be used.

For instance, if you want to use Castle's DynamixProxy, add a reference to NHibernate.ByteCode.Castle.dll to your project and add the following line to your hibernate.cfg.xml file:

    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

If you want to use another proxy factory, add a reference to the correct one (for instance, NHibernate.ByteCode.LinFu.dll) and modify the property in the config file. A proxy factory which uses Spring will also be included soon.

  • http://fabiomaulo.blogspot.com/ Fabio Maulo

    You didn’t see the binaries ;)
    The Bytecode for Sping is there.

    • http://davybrion.com Davy Brion

      ah, i thought it was still left out of the build :)

  • http://dylanbeattie.blogspot.com/ Dylan Beattie

    We hit this exact issue last week – but we’re using ASP.NET MVC and the MvcContrib library in conjunction with Castle ActiveRecord, and immediately following the upgrade, the only error message we got was:

    System.ExecutionEngineException was unhandled
    Message “Exception of type ‘System.ExecutionEngineException’ was thrown.”

    According to MSDN, “execution engine errors are fatal errors that should never occur” – and for some reason, the actual NHibernate config exception wasn’t showing up in the debugger or error messages anywhere – so it took a while to root out the source of the problem. We ended up compiling the entire project from source, including NHibernate and Castle, and this led us the proxy configuration errors described above; hopefully this comment will help anyone else having the same MvcContrib / ExecutionEngineException problems.

  • Pingback: Reflective Perspective - Chris Alcock » The Morning Brew #307

  • Biju Thomas

    Hi,

    Am new to NHibernate development.
    Can u sepecify how we can use NHibernate in our app and what configuarion are requred for NHibernate 2.1 Alpha. Am getting the error mentioned above and i dont know where to configure the property.

    Thanks in advance

  • http://davybrion.com Davy Brion

    that property needs to be added to the hibernate.cfg.xml file, like it says in the post…

  • Aaron Croyle

    @Davy

    I’m new to NHibernate as well, and have my config stuff in web.config. I do not have a hibernate.cfg.xml file, other settings seem to get loaded from web.config, but this does not.

  • http://davybrion.com Davy Brion

    @Aaron

    you can use the web.config to store nhibernate configuration settings but the format is slightly different. Using the hibernate.cfg.xml config file is typically the easiest solution, just make sure it’s marked as content and is copied to the build output.

  • http://fgheysels.blogspot.com Frederik Gheysels

    Hmm, I’m trying to migrate my application to NH2.1 beta, and the first error that comes accross is that the type initializer has thrown an exception while creating the configuration-section handler for hibernate-configuration.
    It cannot load the NHibernate assembly, or one of its dependencies … Strange, it seems that this is an error I have that is not related with the proxy-factory issue, since I’ve added this additional property to my Cfg file (and I’ve added a reference to the desired file as well … )
    (I’ve never received the exception that you’ve mentionned, the first exception that I’ve been given, is the ‘FileNotFoundException’).

  • http://davybrion.com Davy Brion
  • http://fgheysels.blogspot.com Frederik Gheysels

    Stupid me has solved the problem 8)7

    (No, the problem was not related to Antlr3; the exception really said that NHibernate or one of its assemblies could not be loaded).
    In my configSection, I did not specify an exact version. I have referenced the NHibernate assembly from the GAC. Copy local has been set to false on my reference; apparently, the CLR was not able to exactly determine which NHibernate assembly should be loaded. So, I’ve solved it by setting ‘Copy Local’ to true on my NHibernate reference).

    Anyway, interesting blog. :)

  • http://jack-fx.com Jack

    There are many dependency, and we do have 2 choice:
    1. NHibernate.ByteCode.LinFu.ProxyFactoryFactory
    2. NHibernate.ByteCode.Castle.ProxyFactoryFactory

  • Nigrita

    Thx, man. Your article solve my problem )

  • http://www.agileatwork.com Mike

    Thanks, exactly what I needed!

  • Vic

    Hi,

    I’m using Fluent NHibernate. Where i should add the line ?

  • Vic

    I found the solution:
    MsSqlConfiguration.MsSql2005.ConnectionString(c => c.Is(connectionstring)).ProxyFactoryFactory(“NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle”);

  • Gilberto

    I couldn’t find the hibernate.cfg.xml file in my machine. Where is it located?
    Thanks!

  • Bruno

    Hi,

    I put the NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle in hibernate.cfg.xml file, and the reference NHibernate.ByteCode.Castle.dll, but shows this error yet:

    The ProxyFactoryFactory was not configured.
    Initialize ‘proxyfactory.factory_class’ property of the session-factory configuration section with one of the available NHibernate.ByteCode providers.
    Example:
    NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu
    Example:
    NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle

    Is the same error of your post. What do you think that is happenning?

  • Pingback: Upgrading to NHibernate 2.1 for new Validators! « Geospecialling

  • http://www.krokonoster Krokonoster

    I’m curious to know the benefits of the various proxies over each other.
    Anyone got some resources regarding that?