1

试图利用 Castle Windsor IoC。我有一个非常简单的应用程序。我的接口存在于 Test.Services 命名空间中。编译时出现以下异常:

“找不到类型名称 Test.Services.IParse,Test.Services”

这是我的 app.config:

<configuration>
  <configSections>
    <section name="castle"
      type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,    
      Castle.Windsor" />


   </configSections>

  <castle>
    <components>
      <component id="HtmlTitleRetriever"
                 type="Test.HTMLTitleRetriever, Test">
      </component>
      <component id="FileParser"
                 service="Test.Services.IParse, Test.Services"
                 type="Test.FileParser,   
                      Test">
      </component>
      <component id="FileDownloader"
                 service="Test.Services.IDownload, Test.Services"
                 type="Test.FileDownloader, Test">
      </component>
    </components>
  </castle>

有人可以告诉我我错过了什么吗?

谢谢

-缺口

4

5 回答 5

2

愚蠢的问题,但是您确定包含您注册的类的程序集实际上已复制到输出目录吗?

于 2009-03-09T14:11:10.057 回答
0

Test.Services 程序集 (Test.Services.dll) 中是否有 IParse 接口?

用于在配置中定义类型名称的格式与您可以从类型的AssemblyQualifiedName属性中获得的格式相同。简而言之,就是:

命名空间.类型,程序集。

对于泛型类型,请参阅this

于 2009-03-10T00:04:19.677 回答
0
IWindsorContainer container = new WindsorContainer();

        container.AddComponent("FileDownlader", typeof(IDownload),
             typeof(FileDownloader));
        container.AddComponent("FileParser", typeof(IParse),
             typeof(FileParser));
        container.AddComponent("HTMLTitleRetriever", typeof(HTMLTitleRetriever));

        HTMLTitleRetriever retriever = container.Resolve<HTMLTitleRetriever>();

在代码中执行以下操作。我想使用一个配置文件,所以不需要每次都重新编译更改。

于 2009-03-08T21:49:02.553 回答
0

几乎是尼克!您需要将 xml 配置传递给 WindsorContainer 的构造函数:

IWindsorContainer container;
container =
  new WindsorContainer(
    new XmlInterpreter(
      new ConfigResource("castle")
  ));

当我第一次开始玩 Castle Windsor 时,我也遇到了这个“Got ya”。它不知道查看配置部分。

提示:去他们的后备箱获取最新信息。有一个名为 Register 的新方法对于在同一个接口上批量注册多个类非常有用。即,如果您正在使用 ASP.NET MVC 进行开发,并且您有一堆 IController,您可以使用 Castle Windsor 为您自动注册它们。因此,您不必在配置文件中指定它们!

public class WindsorControllerFactory : IControllerFactor
{
  IWindsorContainer container;
  public WindsorControllerFactory()
  {
    container =
      new WindsorContainer(
        new XmlInterpreter(
          new ConfigResource("castleWindsor")
      ));

    container.Register(
      AllTypes.Of<IController>()
      .FromAssembly(Assembly.GetExecutingAssembly())
      .Configure(component => component.LifeStyle.Transient
        .Named(component.Implementation.Name)
      ));
  }
}
于 2009-03-08T21:58:41.937 回答
0

我遇到了同样的问题,Eric 的解决方案可以解决这个问题。我创建了一个“域模型”项目,然后将其重命名为“域模型”,尽管我更改了所有引用,但随后查看 bin 文件夹,该程序集仍被称为“域模型”,这就是温莎城堡找不到它的原因。

于 2009-07-29T14:29:12.677 回答