无法从组装错误中加载类型
在尝试学习温莎城堡的Fluent界面时,我编写了以下简单测试:
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); } } }
通过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()
通过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.
如果打开我在Reflector中引用的程序集,则可以看到其信息是:
Castle.MicroKernel, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc
并且它肯定包含Castle.MicroKernel.Registration.IRegistration
可能是怎么回事?
我应该提到的是,这些二进制文件是从Castle的最新版本中获取的,尽管我从未与nant一起工作过,所以我不必从源代码中重新编译,只需将文件放在bin目录中。我还应该指出,我的项目编译没有问题。
解决方案
程序集是否在全局程序集缓存(GAC)中或者可能覆盖我们认为正在加载的程序集的任何位置?这通常是由于加载了错误的程序集导致的,对我而言,这意味着我通常在GAC中具有某些内容,这些内容将覆盖bin / Debug中的版本。
版本= 1.0.3.0表示Castle RC3,但是在RC3发行几个月后便开发出了流畅的界面。因此,看起来我们有版本控制问题。也许我们已在GAC中注册了Castle RC3,并且正在使用该城堡...
如果我们有一个项目引用了另一个项目(例如,引用了"类库"的" Windows应用程序"类型)并且都具有相同的程序集名称,则将收到此错误。我们可以使用强名称来命名引用的项目,也可以(甚至更好)重命名引用项目的程序集(在VS中项目属性的"应用程序"选项卡下)。
我偶尔会收到这个消息,并且GAC中的程序集总是很糟糕
很抱歉疏通了一个古老的答案,但是上面没有提到针对我的解决方案,所以我想我会把我的答案加到长尾巴上...
我最终在web.config中有一个旧的对类(HttpHandler)的引用,该引用不再使用(并且不再是有效的引用)。由于某种原因,它在Studio中运行时被忽略了(或者也许我的开发设置中仍可以访问该类?),因此,当我尝试部署到IIS时,才出现此错误。我在web.config中搜索了程序集名称,删除了未使用的处理程序引用,然后该错误消失了,一切正常。希望这对其他人有帮助。