我写了以下简单的测试来学习Castle Windsor的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 目录中拿了这些文件。另外,我应该指出,我的项目编译没有问题。