C#:无法从程序集中加载类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10297961/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:14:58  来源:igfitidea点击:

C#: Could not load types from assembly

c#windowsvisual-studiodlllucene.net

提问by Saustrup

After adding Lucene.net and Lucene.net Contrib to a C# MVC3, I get the message below after the first successful run. After receiving this error, I need to completely wipe C:\Users\Me\AppData\Local\Temp\Temporary ASP.NET Files before I can run the project again.

将 Lucene.net 和 Lucene.net Contrib 添加到 C# MVC3 后,我在第一次成功运行后收到以下消息。收到此错误后,我需要完全擦除 C:\Users\Me\AppData\Local\Temp\Temporary ASP.NET Files,然后才能再次运行该项目。

I've tried removing the Lucene files manually (including references in my project), and reinstalling them - both with NuGet and manually - but it's always the same situation; after the project has been run once, I start getting the following errors:

我已经尝试手动删除 Lucene 文件(包括我的项目中的引用),并重新安装它们 - 使用 NuGet 和手动 - 但它总是相同的情况;项目运行一次后,我开始收到以下错误:

Note: Contrib.Regex is part of Lucene.net Contrib.

注意:Contrib.Regex 是 Lucene.net Contrib 的一部分。

Server Error in '/' Application.

Could not load types from assembly Contrib.Regex, Version=2.9.4.0, Culture=neutral, PublicKeyToken=85089178b9ac3181, errors:
Exception: System.IO.FileLoadException: Could not load file or assembly 'Lucene.Net, Version=2.9.4.1, Culture=neutral, PublicKeyToken=85089178b9ac3181' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'Lucene.Net, Version=2.9.4.1, Culture=neutral, PublicKeyToken=85089178b9ac3181'

=== Pre-bind state information ===
LOG: User = rcw7\Me
LOG: DisplayName = Lucene.Net, Version=2.9.4.1, Culture=neutral, PublicKeyToken=85089178b9ac3181
(Fully-specified)
LOG: Appbase = file:///C:/Development/Projects/Foobar/Foobar/
LOG: Initial PrivatePath = C:\Development\Projects\Foobar\Foobar\bin
Calling assembly : Contrib.Regex, Version=2.9.4.0, Culture=neutral, PublicKeyToken=85089178b9ac3181.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Development\Projects\Foobar\Foobar\web.config
LOG: Using host configuration file: C:\Users\Me\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Lucene.Net, Version=2.9.4.1, Culture=neutral, PublicKeyToken=85089178b9ac3181
LOG: Attempting download of new URL file:///C:/Users/Me/AppData/Local/Temp/Temporary ASP.NET Files/root/e9b4cfa4/edfa73f8/Lucene.Net.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Revision Number
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
Exception: System.IO.FileLoadException: Could not load file or assembly 'Lucene.Net, Version=2.9.4.1, Culture=neutral, PublicKeyToken=85089178b9ac3181' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'Lucene.Net, Version=2.9.4.1, Culture=neutral, PublicKeyToken=85089178b9ac3181'

Full output here: http://pastebin.com/Vbu4VK7B

完整输出在这里:http: //pastebin.com/Vbu4VK7B

Initially I thought this was a problem local to my development environment, but after rebuilding and copying the project to our server, I get the same errors.

最初我认为这是我的开发环境的本地问题,但是在重建项目并将其复制到我们的服务器后,我遇到了同样的错误。

Any suggestions as how to overcome this? :-)

关于如何克服这个问题的任何建议?:-)

采纳答案by Saustrup

As it turns out, the DLL hell wasn't caused by my own meddling in configurations and compiled libraries. It was in fact caused by a collision between my newly downloaded version of Lucene.NET and a somewhat dated version bundled with Examine, which in turn was bundled with Umbraco 5.

事实证明,DLL 地狱并不是由我自己对配置和编译库的干预造成的。它实际上是由我新下载的 Lucene.NET 版本与 Examine 捆绑的有点过时的版本之间的冲突引起的,后者又与 Umbraco 5 捆绑在一起。

Apparently the outdated, bundled Lucene.NET ended up in the shadow cache (Temporary ASP.NET Files), so after the next compile or IIS restart, execution would break. Clearing the shadow cache would allow for one success execution.

显然,过时的、捆绑的 Lucene.NET 最终出现在影子缓存(临时 ASP.NET 文件)中,因此在下一次编译或 IIS 重新启动后,执行将中断。清除影子缓存将允许一次成功执行。

The weird thing was, that I was unable to find any reference anywhere in the debug output that hinted to the outdated version of Lucene.NET, either with a directory path or version number. The problem was spotted by comparing the file sizes of the shadow copied version of Lucene.NET and the version I intended to use. They were off, so I did a search for Lucene.NET.dll and found the one bundled with Examine in the Umbraco tree (under \App_Plugins\Examine)

奇怪的是,我无法在调试输出中的任何地方找到任何暗示 Lucene.NET 过时版本的参考,无论是目录路径还是版本号。问题是通过比较 Lucene.NET 的影子复制版本和我打算使用的版本的文件大小来发现的。他们关闭了,所以我搜索了 Lucene.NET.dll 并在 Umbraco 树中找到了与 Examine 捆绑在一起的那个(在 \App_Plugins\Examine 下)

The fast solution was to simply zip the Examine plugin, so Umbraco wouldn't see it. That leaves me without the Examine plugin, but I wasn't using it anyway.

快速的解决方案是简单地压缩 Examine 插件,这样 Umbraco 就不会看到它。这让我没有了 Examine 插件,但无论如何我都没有使用它。

The rightsolution would probably be to tell the app that it should ignore previous versions of Lucene.NET, but I haven't had any luck with that so far. This is what I added to web.config:

正确的解决方案可能会告诉应用程序,它应该忽略Lucene.NET的早期版本,但我还没有与任何运气至今。这是我添加到 web.config 的内容:

<dependentAssembly>
    <assemblyIdentity name="Lucene.Net" publicKeyToken="85089178b9ac3181" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-2.9.4.1" newVersion="2.9.4.1" />
</dependentAssembly>

This didn't seem to have any effect, and the outdated version still ended up in the shadow cache. I've moved this question here: How do I make Umbraco 5 ignore the bundled (with Examine) Lucene.NET

这似乎没有任何影响,过时的版本仍然在影子缓存中结束。我已将这个问题移至此处:How do I make Umbraco 5 ignore the bundled (with Examine) Lucene.NET

Thanks for your help and suggestions - it pointed me in the right direction! :-)

感谢您的帮助和建议 - 它为我指明了正确的方向!:-)

回答by Alex Mendez

This normally means that you have the files in the GAC which are a different version and the ones in your bin are another version. So your application is pointing to a version and it is finding another. It is getting confused. What you want to do is uninstall Lucene.net. After you uninstall, look in the c:\windows\assembly folder and make sure there are no Lucene files in there. If there are, right click and uninstall. Then you can install again.

这通常意味着 GAC 中的文件是不同版本,而 bin 中的文件是另一个版本。所以你的应用程序指向一个版本,它正在寻找另一个版本。它越来越困惑。您要做的是卸载 Lucene.net。卸载后,查看 c:\windows\assembly 文件夹并确保其中没有 Lucene 文件。如果有,请右键单击并卸载。然后您可以再次安装。

回答by RajN

You can use the Fusion Log Viewerto diagnose this problems. It gives you full details of the types failed to load similar to the one you reported here. Hope this helps.

您可以使用Fusion Log Viewer来诊断此问题。它为您提供了无法加载的类型的完整详细信息,类似于您在此处报告的类型。希望这可以帮助。

回答by Jim Schubert

The manifest definition does not matchmeans there is an issue with the resolution of the assembly. see this SO question

manifest definition does not match意味着大会的决议存在问题。 看到这个问题

[edit]

[编辑]

Assemblies are loaded from GAC, then lib/bin directories within your application, then directories specified via <HintPath>in your project. Check that you have

程序集从 GAC 加载,然后是应用程序中的 lib/bin 目录,然后是通过<HintPath>项目中指定的目录。检查你是否有

Specific Version = Trueand Copy Local = True

Specific Version = TrueCopy Local = True

in the reference's properties window.

在参考的属性窗口中。

Assemblies from the private paths within your application (lib/bin directories) are the only ones that get shadow-copied. It's possible Contrib.Regex is being shadow-copied while Lucene.NET core is not.

来自您的应用程序(lib/bin 目录)中的私有路径的程序集是唯一被影子复制的程序集。Contrib.Regex 可能被影子复制,而 Lucene.NET 核心没有。

[/edit]

[/编辑]

The directory under ASP.NET Temporary Filesthat you are wiping is the shadow-copy directory. There may be an issue with how this assembly is being copied, which maybe permissions for your domain account. You can test this theory by changing the shadowcopy directory or turning off shadowcopying completely as described here:

ASP.NET Temporary Files您正在擦除的目录是 shadow-copy 目录。此程序集的复制方式可能存在问题,这可能是您的域帐户的权限。您可以通过改变影拷贝目录或关闭所描述的完全shadowcopying验证这一理论在这里

You can change this directory in Application_Start as explained at https://stackoverflow.com/a/2847495/151445

You can turn off shadow copying in the web.config:

<hostingEnvironment shadowCopyBinAssemblies="false" />

您可以按照https://stackoverflow.com/a/2847495/151445 中的说明在 Application_Start 中更改此目录

您可以在 web.config 中关闭卷影复制:

<hostingEnvironment shadowCopyBinAssemblies="false" />