Could not load file or assembly in NHibernate

15

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?

c#
asp.net
nhibernate
asked on Stack Overflow Oct 13, 2009 by GaryDevenay • edited Apr 29, 2012 by Peter Mortensen

14 Answers

14

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
answered on Stack Overflow Oct 15, 2009 by josemrb • edited Apr 29, 2012 by Peter Mortensen
6

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.

answered on Stack Overflow Mar 14, 2011 by Markus Bruckner • edited Apr 3, 2019 by Markus Bruckner
5

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.

answered on Stack Overflow Oct 15, 2009 by CubanX
4

I had this problem as well.

For me, the issue was that FluentNHibernate expected a different version of NHibernate (2.1.0.4000) than I was including in the project (2.1.2.4000). I stumbled on this by separately downloading the latest releases of each library.

To fix the issue, I changed my NHibernate reference to point to the older version of NHibernate that came with FluenNHibernate 1.0 RTM (2.1.0.4000).

Another solution may be to explicitly set your assembly bindings from the app.config file.

answered on Stack Overflow Mar 10, 2010 by Randy Klingelheber
3

In my case, "Clean Solution", followed by "Rebuild Solution" solved the problem.

answered on Stack Overflow Oct 27, 2009 by Tom Bushell
2

I recently upgraded our project with the 1.0 RTM version of FluentNHibernate, which required the latest NHibernate bits. This led to the same problem you are having.

Our project's structure was something like this:

Repository root
    Solution
        Web
            References
                DataAccess
        ... other projects/layers ...
        DataAccess
            References
                ..\ReferenceAssemblies\NHibernate.dll
                ..\ReferenceAssemblies\FluentNHibernate.dll
    ReferenceAssemblies

(All external DLL's reside in the ReferenceAssemblies directory.)

My first attempt to solve the problem was by adding a reference to NHibernate.ByteCode.Castle.dll to the DataAccess project. This worked... but only in development...

When I published the web application to our customer acceptance test server (which happens automatically with the help of TeamCity and a script containing a call to aspnet_compiler.exe), the NHibernate.ByteCode.Castle.dll was nowhere to be found.

I am not sure why this is happening, but I suspect that it has something to do with the fact that no code whatsoever in our application actually calls code in that specific dll. Also, there's (correct me if I'm wrong) no hardcoded reference from NHibernate.dll to NHibernate.ByteCode.Castle, so somewhere down the line the (presumably unused) dll is overlooked.

The second (and successful) attempt was to add a reference to the missing dll directly to the web project. Now, I could remove the reference I added in the first attempt without any problems.

(I'm not particularly fond of having such a reference in that particular project, but hey!) :-)

answered on Stack Overflow Oct 15, 2009 by Christoffer Lette • edited Oct 15, 2009 by Christoffer Lette
1

You probably have code referencing two different versions of the NHibernate DLL. The NHibernate.dll that you download with NHibernate is likely a different version from the one you download with, say, Castle ActiveRecord. Try to stick with just the version of the NHibernate DLL that came with NHibernate.ByteCode.Castle. And make sure you don't have the NHibernate DLL (any version) in your GAC (at least until you get this problem resolved).

answered on Stack Overflow Oct 14, 2009 by Michael Maddox
1

I had this problem after upgrading NHibernate to 3.0 and Spring to nightly build which used NH 3.0. The problem was that Spring.Data.NHibernate30.dll referenced older version of NHibernate.dll (v 3.0.0.2002) and I had v 3.0.0.4000.

My solution was to open project Spring.Data.NHibernate30 source, remove references to older version of NHibernate, add reference to version 3.0.0.4000, rebuild and now in my project add reference to this (newly built) version of Spring.Data.NHibernate30.dll.

answered on Stack Overflow Dec 9, 2010 by dabor
1

I just had this happen to me -- in my case this was my first time trying out NHibernate (and Fluent NHibernate). I had implemented most of my application in a .dll assembly, referenced by a separate executable assembly. The executable, which didn't have references to NHibernate directly, was giving me the error.

I'm using NuGet to pull down Fluent NHibernate, so I added the Fluent NHibernate reference to the executable assemblies as well. Doesn't feel super clean, but it gets the dependencies into the .bin folder and resolves the error. (funny that it doesn't (seem to?) happen with other libraries, like NLog)

answered on Stack Overflow Mar 27, 2011 by Luke Winikates
1

I was facing the same issue. I cleared ''Temporary Internet Files'', and this issue went away.

answered on Stack Overflow Aug 20, 2011 by Bhoomi • edited Apr 29, 2012 by Peter Mortensen
0

NHibernate has a few other assemblies other than nhibernate.dll. Did you have all of them in (from the same release)?

answered on Stack Overflow Oct 13, 2009 by o.k.w • edited Apr 29, 2012 by Peter Mortensen
0

You should reference the dependent assemblies (I guess it's "NHibernate.ByteCode.Castle") and set their "copy local" attribute to true.

answered on Stack Overflow Oct 13, 2009 by Meidan Alon
0

You might have different projects in your solution. Those project might be referencing different versions of NHibernate.dll. Checks versions in all projects and rebuild the project

answered on Stack Overflow Oct 25, 2013 by NoloMokgosi
-1

It looks like the NHibernate dll isn't being copied to the bin directory of your application. Make sure it's referenced and exists in the bin directory.

answered on Stack Overflow Oct 13, 2009 by lomaxx

User contributions licensed under CC BY-SA 3.0