Castle Windsor无法找到我的服务类型

时间:2009-03-08 21:20:21

标签: c# inversion-of-control castle-windsor

尝试使用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>

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

由于

-Nick

5 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分: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>();

在代码中执行以下操作。我想使用Config文件,但每次都不需要重新编译。

答案 2 :(得分:0)

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

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

当我第一次开始和温莎城堡一起玩时,我也遇到了这个“得到你”。它不知道要查看配置部分。

提示:从他们的行李箱中获取最新消息。有一个名为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)
      ));
  }
}

答案 3 :(得分:0)

Test.Services程序集中的IParse接口(Test.Services.dll)?

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

Namespace.Type,Assembly。

有关通用类型,请参阅this

答案 4 :(得分:0)

我遇到了同样的问题,Eric的解决方案也解决了这个问题。我创建了一个“域模型”项目,然后将其重命名为“DomainModel”我虽然我更改了所有引用但是然后查看bin文件夹,程序集仍被称为“域模型”,这就是为什么城堡windsor找不到它。