Friday, February 25, 2011

[General-Dev]–FluentNHibernate cannot load NHibernate assembly when setup with NuGet

I ran into this issue today after using NuGet for the first time. I started a new project using NHibernate and I wanted to configure my mappings using Fluent NHibernate. NuGet makes setting up your references really easily, by right clicking on my references folder and going to Add Library Package Reference I could easily search for NHibernate and Fluent NHibernate and install them.

image

Very impressive – and certainly much easier than on my last NHibernate project which pushed me into DLL hell to get it setup! But, the DLL pain wasn’t quite gone – when trying to run my project after setting up the mappings I got the following error:

Could not load file or assembly 'NHibernate, Version=3.0.0.2001, Culture=neutral, PublicKeyToken=aa95f207798dfdb4' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

What? Isn’t NuGet meant to save me from this? well, turns out that it does, but I have to give it a push. To work around this, I had to open up the Package Manager Console (Tools > Library Package Manager > Package Manager Console) and typed the following:

Add-BindingRedirect

This redirects the bindings to the newer version of NHibernate which was installed by NuGet, whereas the Fluent NHibernate references was pointing to a slightly earlier build. I’m not sure why NuGet doesn’t do this by default, but this Stack Overflow answer seems to suggest they will do this soon.

No comments: