.net 无法从程序集错误中加载类型

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

Could not load type from assembly error

.net

提问by George Mauer

I have written the following simple test in trying to learn Castle Windsor's Fluent Interface:

我已经编写了以下简单的测试来尝试学习 Castle Windsor 的 Fluent Interface:

using NUnit.Framework;
using Castle.Windsor;
using System.Collections;
using Castle.MicroKernel.Registration;

namespace WindsorSample {
    public class MyComponent : IMyComponent {
        public MyComponent(int start_at) {
            this.Value = start_at;
        }
        public int Value { get; private set; }
    } 
    public interface IMyComponent {
        int Value { get; }
    }

    [TestFixture]
    public class ConcreteImplFixture {
        [Test]
        public void ResolvingConcreteImplShouldInitialiseValue() {
            IWindsorContainer container = new WindsorContainer();
            container.Register(Component.For<IMyComponent>().ImplementedBy<MyComponent>().Parameters(Parameter.ForKey("start_at").Eq("1")));
            IMyComponent resolvedComp = container.Resolve<IMyComponent>();
            Assert.AreEqual(resolvedComp.Value, 1); 
        }
    }
}

When I execute the test through TestDriven.NET I get the following error:

当我通过 TestDriven.NET 执行测试时,出现以下错误:

System.TypeLoadException : Could not load type 'Castle.MicroKernel.Registration.IRegistration' from assembly 'Castle.MicroKernel, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc'.
at WindsorSample.ConcreteImplFixture.ResolvingConcreteImplShouldInitialiseValue()

When I execute the test through the NUnit GUI I get:

当我通过 NUnit GUI 执行测试时,我得到:

WindsorSample.ConcreteImplFixture.ResolvingConcreteImplShouldInitialiseValue:
System.IO.FileNotFoundException : Could not load file or assembly 'Castle.Windsor, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc' or one of its dependencies. The system cannot find the file specified.

If I open the Assembly that I am referencing in Reflector I can see its information is:

如果我打开我在 Reflector 中引用的程序集,我可以看到它的信息是:

Castle.MicroKernel, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc

and that it definitely contains Castle.MicroKernel.Registration.IRegistration

并且它肯定包含Castle.MicroKernel.Registration.IRegistration

What could be going on?

会发生什么?

I should mention that the binaries are taken from the latest build of Castlethough I have never worked with nant so I didn't bother re-compiling from source and just took the files in the bin directory. I should also point out that my project compiles with no problem.

我应该提到二进制文件是从最新版本的 Castle 中获取的,尽管我从未使用过 nant,所以我没有费心从源代码重新编译,只是获取了 bin 目录中的文件。我还应该指出我的项目编译没有问题。

采纳答案by Eric Schoonover

Is the assembly in the Global Assembly Cache (GAC) or any place the might be overriding the assembly that you think is being loaded? This is usually the result of an incorrect assembly being loaded, for me it means I usually have something in the GAC overriding the version I have in bin/Debug.

程序集是否位于全局程序集缓存 (GAC) 或任何可能覆盖您认为正在加载的程序集的地方?这通常是加载了错误程序集的结果,对我来说,这意味着我通常在 GAC 中有一些东西覆盖了我在 bin/Debug 中的版本。

回答by AndrewS

If you have one project referencing another project (such as a 'Windows Application' type referencing a 'Class Library') and both have the same Assembly name, you'll get this error. You can either strongly name the referenced project or (even better) rename the assembly of the referencing project (under the 'Application' tab of project properties in VS).

如果您有一个项目引用另一个项目(例如引用“类库”的“Windows 应用程序”类型)并且两者具有相同的程序集名称,则会出现此错误。您可以强命名引用的项目,或者(甚至更好)重命名引用项目的程序集(在 VS 中项目属性的“应用程序”选项卡下)。

回答by Brian Moeskau

The solution to this for me was not mentioned above, so I thought I would add my answer to the long tail...

上面没有提到我的解决方案,所以我想我会把我的答案添加到长尾......

I ended up having an old reference to a class (an HttpHandler) in web.config that was no longer being used (and was no longer a valid reference). For some reason it was ignored while running in Studio (or maybe I have that class still accessible within my dev setup?) and so I only got this error once I tried deploying to IIS. I searched on the assembly name in web.config, removed the unused handler reference, then this error went away and everything works great. Hope this helps someone else.

我最终HttpHandler在 web.config 中对不再使用的类 (an )进行了旧引用(并且不再是有效引用)。出于某种原因,它在 Studio 中运行时被忽略了(或者我的开发设置中可能仍然可以访问该类?)所以我只有在尝试部署到 IIS 时才收到此错误。我在 web.config 中搜索程序集名称,删除了未使用的处理程序引用,然后这个错误消失了,一切正常。希望这对其他人有帮助。

回答by Matthias Wolf

I had the same issue and for me it had nothing to do with namespace or project naming.

我有同样的问题,对我来说它与命名空间或项目命名无关。

But as several users hinted at it had to do with an old assembly still being referenced.

但正如一些用户所暗示的那样,它与仍在引用的旧程序集有关。

I recommend to delete all "bin"/binary folders of all projects and to re-build the whole solution. This washed out any potentially outdated assemblies and after that MEF exported all my plugins without issue.

我建议删除所有项目的所有“bin”/binary 文件夹并重新构建整个解决方案。这清除了任何可能过时的程序集,之后 MEF 毫无问题地导出了我的所有插件。

回答by Michael Maddox

I was getting this error and nothing I found on StackOverflow or elsewhere solved it, but bmoeskau's answerto this question pointed me in the right direction for the fix, which hasn't been mentioned yet as an answer. My answer isn't strictly related to the original question, but I'm posting it here under the assumption that someone having this problem will find there way here through searching Google or something similar (like myself one month from now when this bites me again, arg!).

我遇到了这个错误,我在 StackOverflow 或其他地方找不到任何解决方法,但是bmoeskau对这个问题的回答为我指明了正确的修复方向,但尚未作为答案提及。我的答案与原始问题并不严格相关,但我将其发布在这里是假设遇到此问题的人会通过搜索谷歌或类似的东西在这里找到方法(就像我一个月后当这再次咬我时) ,arg!)。

My assembly is in the GAC, so there is theoretically only one version of the assembly available. ExceptIIS is helpfully caching the old version and giving me this error. I had just changed, rebuilt and reinstalled the assembly in the GAC. One possible solution is to use Task Manager to kill w3wp.exe. This forces IIS to reread the assembly from the GAC: problem solved.

我的程序集在 GAC 中,因此理论上只有一个版本的程序集可用。 除了IIS 有助于缓存旧版本并给我这个错误。我刚刚更改、重建并重新安装了 GAC 中的程序集。一种可能的解决方案是使用任务管理器杀死w3wp.exe。这会强制 IIS 从 GAC 重新读取程序集:问题已解决。

回答by Mauricio Scheffer

Version=1.0.3.0 indicates Castle RC3, however the fluent interface was developed some months after the release of RC3. Therefore, it looks like you have a versioning problem. Maybe you have Castle RC3 registered in the GAC and it's using that one...

Version=1.0.3.0 表示 Castle RC3,但是流畅的界面是在 RC3 发布几个月后开发的。因此,您似乎存在版本控制问题。也许你已经在 GAC 中注册了 Castle RC3,它正在使用那个......

回答by SteveC

I get this occasionally and it's always been down to have the assembly in the GAC

我偶尔会遇到这个问题,但在 GAC 中召开大会总是失败的

回答by Iadine GOUNDETE

If this error caused by changing the namespace, make sur that the folder of that project is renamed to the same name, and close VS.NET Edit the project which has the problem with Notepad and replace there nodes

如果这个错误是因为更改命名空间引起的,请确保该项目的文件夹重命名为相同的名称,并关闭VS.NET 用记事本编辑有问题的项目并替换那里的节点

"RootNamespace>New_Name_Of_Folder_Of_Your_Project_Namespace"RootNamespace> "AssemblyName>New_Name_Of_Folder_Of_Your_Project_Namespace"AssemblyName>

"RootNamespace>New_Name_Of_Folder_Of_Your_Project_Namespace"RootNamespace> "AssemblyName>New_Name_Of_Folder_Of_Your_Project_Namespace"AssemblyName>

回答by Shelby115

Deleting my .pdb file for the dll solved this issue for me. I'm guessing it has something to do with the fact that the dll was created using ILMerge.

删除 dll 的 .pdb 文件为我解决了这个问题。我猜这与使用 ILMerge 创建 dll 的事实有关。

回答by Jakub Szumiato

When I run into such problem, I find FUSLOGVW tool very helpful. It is checking assembly binding information and logs it for you. Sometimes the libraries are missing, sometimes GAC has different versions that are being loaded. Sometimes the platform of referenced libraries is causing the problems. This tool makes it clear how the dependencies' bindings are being resolved and this may really help you to investigate/debug your problem.

当我遇到这样的问题时,我发现 FUSLOGVW 工具非常有用。它正在检查程序集绑定信息并为您记录。有时缺少库,有时 GAC 加载了不同的版本。有时,引用库的平台会导致问题。这个工具清楚地说明了依赖项的绑定是如何解决的,这可能真的有助于你调查/调试你的问题。

Fusion Log Viewer / fuslogvw / Assembly Binding Log Viewer. Check more/download here: http://msdn.microsoft.com/en-us/library/e74a18c4.aspx.

融合日志查看器 / fuslogvw / 程序集绑定日志查看器。在此处查看更多/下载:http: //msdn.microsoft.com/en-us/library/e74a18c4.aspx