C# 无法在 NHibernate 中加载文件或程序集
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1560050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not load file or assembly in NHibernate
提问by GaryDevenay
I recently had some problems with the hibernate.cfg.xml
file as I hadn't had the following line in.
我最近在hibernate.cfg.xml
文件方面遇到了一些问题,因为我没有输入以下行。
<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)
无法加载文件或程序集“NHibernate,版本=2.1.0.4000,文化=中性,PublicKeyToken=aa95f207798dfdb4”或其依赖项之一。定位的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)
Why do I get this error and how do I fix it?
为什么会出现此错误以及如何修复?
采纳答案by josemrb
These files should be in the same directory as the referenced file, NHibernate.dll:
这些文件应该与引用的文件 NHibernate.dll 位于同一目录中:
Antlr3.Runtime.dll
Iesi.Collections.dll
log4net.dll
Castle.Core.dll
Castle.DynamicProxy2.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
- NHibernate.ByteCode.Castle.dll
回答by lomaxx
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.
看起来 NHibernate dll 没有被复制到应用程序的 bin 目录中。确保它被引用并存在于 bin 目录中。
回答by o.k.w
NHibernate has a few other assemblies other than nhibernate.dll
. Did you have all of them in (from the same release)?
除了nhibernate.dll
. 你有所有这些(来自同一个版本)吗?
回答by Meidan Alon
You should reference the dependent assemblies (I guess it's "NHibernate.ByteCode.Castle") and set their "copy local" attribute to true.
您应该引用依赖程序集(我猜它是“NHibernate.ByteCode.Castle”)并将它们的“复制本地”属性设置为 true。
回答by Michael Maddox
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).
您可能有引用两个不同版本的 NHibernate DLL 的代码。您使用 NHibernate 下载的 NHibernate.dll 可能与您使用 Castle ActiveRecord 下载的版本不同。尽量坚持使用 NHibernate.ByteCode.Castle 附带的 NHibernate DLL 版本。并确保您的 GAC 中没有 NHibernate DLL(任何版本)(至少在您解决此问题之前)。
回答by Christoffer Lette
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.
我最近使用 FluentNHibernate 的 1.0 RTM 版本升级了我们的项目,这需要最新的 NHibernate 位。这导致了您遇到的同样问题。
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.)
(所有外部 DLL 都位于 ReferenceAssemblies 目录中。)
My first attemptto solve the problem was by adding a reference to NHibernate.ByteCode.Castle.dll to the DataAccess project. This worked... but only in development...
我第一次尝试解决这个问题是通过向 DataAccess 项目添加对 NHibernate.ByteCode.Castle.dll 的引用。这有效......但仅限于开发......
When I published the web application to our customer acceptance test server (which happens automatically with the help of TeamCityand a script containing a call to aspnet_compiler.exe), the NHibernate.ByteCode.Castle.dll was nowhere to be found.
当我将 Web 应用程序发布到我们的客户验收测试服务器(在TeamCity和包含对 aspnet_compiler.exe 调用的脚本的帮助下自动发生)时,找不到 NHibernate.ByteCode.Castle.dll。
I am not sure whythis 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.
我不确定为什么会发生这种情况,但我怀疑这与我们的应用程序中没有任何代码实际调用该特定 dll 中的代码的事实有关。此外,(如果我错了,请纠正我)没有从 NHibernate.dll 到 NHibernate.ByteCode.Castle 的硬编码引用,因此在某处(可能未使用的)dll 被忽略了。
The second (and successful)attemptwas 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.
第二次(也是成功的)尝试是将缺少的 dll 的引用直接添加到 Web 项目中。现在,我可以删除我在第一次尝试中添加的引用,没有任何问题。
(I'm not particularly fond of having such a reference in that particular project, but hey!) :-)
(我不是特别喜欢在那个特定项目中有这样的参考,但是嘿!):-)
回答by CubanX
I'm assuming you recently upgraded NHibernate to 2.1?
我假设您最近将 NHibernate 升级到 2.1?
If so, my guess is you have different projects referencing different versions of NHibernate.
如果是这样,我的猜测是您有不同的项目引用了不同版本的 NHibernate。
This happened to me and is harder to track down than you might think.
这发生在我身上,比你想象的更难追踪。
These are the steps I took to solve it:
这些是我为解决它而采取的步骤:
- 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
- Edit all your .csproj files. Edit them either with a text editor or do the Unload Project then edit your .csproj file.
- Make sure ALL your HintPath nodes point to the same (new) version of the DLL
- 删除项目中所有 bin 目录中的所有文件。通常 Clean Solution 对此很有效,但事实并非如此,您可能必须通过命令行调用或手动执行此操作
- 编辑所有 .csproj 文件。使用文本编辑器编辑它们或执行卸载项目,然后编辑您的 .csproj 文件。
- 确保所有 HintPath 节点都指向相同(新)版本的 DLL
That will hopefully clear up this issue for you.
这有望为您解决这个问题。
回答by Tom Bushell
In my case, "Clean Solution", followed by "Rebuild Solution" solved the problem.
就我而言,“清洁解决方案”,然后是“重建解决方案”解决了问题。
回答by Randy Klingelheber
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.
对我来说,问题是 FluentNHibernate 期望的 NHibernate (2.1.0.4000) 版本与我在项目中包含的版本 (2.1.2.4000) 不同。我通过单独下载每个库的最新版本偶然发现了这一点。
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).
为了解决这个问题,我更改了我的 NHibernate 引用以指向 FluenNHibernate 1.0 RTM (2.1.0.4000) 附带的旧版本 NHibernate。
Another solution may be to explicitly set your assembly bindings from the app.config file.
另一种解决方案可能是从 app.config 文件中显式设置程序集绑定。
回答by dabor
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.
在将 NHibernate 升级到 3.0 并将 Spring 升级到使用 NH 3.0 的每晚构建后,我遇到了这个问题。问题是 Spring.Data.NHibernate30.dll 引用了旧版本的 NHibernate.dll (v 3.0.0.2002) 而我有 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.
我的解决方案是打开项目 Spring.Data.NHibernate30 源,删除对旧版本 NHibernate 的引用,添加对版本 3.0.0.4000 的引用,重建,现在在我的项目中添加对 Spring.Data.NHibernate30 的这个(新建)版本的引用.dll。