Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not load file or assembly in NHibernate

I recently had some problems with the hibernate.cfg.xml file as I hadn't had the following line in.

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

Now that this is fixed I get the following error.

Could not load file or assembly 'NHibernate, Version=2.1.0.4000, 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)

Why do I get this error and how do I fix it?

like image 755
GaryDevenay Avatar asked Oct 13 '09 12:10

GaryDevenay


3 Answers

These files should be in the same directory as the referenced file, NHibernate.dll:

  • Antlr3.Runtime.dll
  • Iesi.Collections.dll
  • log4net.dll
  • Castle.Core.dll
  • Castle.DynamicProxy2.dll

Also you should add a reference or copy this one too:

  • NHibernate.ByteCode.Castle.dll
like image 76
josemrb Avatar answered Nov 10 '22 17:11

josemrb


As a future reference: If your're experiencing the same issues Randy Klingelheber pointed out (dependency problems between NHibernate and FluentNHibernate, or any other dependent library), you can specify a redirection for the assemblies that target the old version in app.config. This prevents one from having to recompile the dependent assembly.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="NHibernate" publicKeyToken="aa95f207798dfdb4" />
      <bindingRedirect oldVersion="3.0.0.3001" newVersion="3.0.0.4000" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

This code redirects requests for the old version (3.0.0.3001 in my case) to the one actually used (3.0.0.4000). The publicKeyToken is included in the error message.

like image 44
Markus Bruckner Avatar answered Nov 10 '22 15:11

Markus Bruckner


I'm assuming you recently upgraded NHibernate to 2.1?

If so, my guess is you have different projects referencing different versions of NHibernate.

This happened to me and is harder to track down than you might think.

These are the steps I took to solve it:

  1. Delete all files in all bin directories in your projects. Usually Clean Solution works well for this, but it doesn't, you may have to do it with a command line call or by hand
  2. Edit all your .csproj files. Edit them either with a text editor or do the Unload Project then edit your .csproj file.
  3. Make sure ALL your HintPath nodes point to the same (new) version of the DLL

That will hopefully clear up this issue for you.

like image 5
CubanX Avatar answered Nov 10 '22 17:11

CubanX