根据装配清单,Castle Windsor对依赖项的要求是什么?

时间:2012-10-30 13:17:22

标签: castle-windsor castle

更新:我没有提到,我正在使用Castle版本2.5.1.0(有许多依赖关系意味着我们没有更新的东西)

我一直在使用一些利用Castle Windsor的框架,并且运气很好。在某些情况下,我遇到了一些情况,当我尝试从AppConfig安装时因为一些缺少引用而出现异常。据我所知,当我做container.Install(Configuration.FromAppConfig());之类的事情时,Castle会加载配置文件中注册的所有组件。从包含已注册组件的每个程序集中,我相信Castle将检查该程序集的清单,然后加载这些程序集,并以此方式继续,直到加载所有必需的程序集。

这导致了我遇到的问题。我用单元测试编写了一些代码。该代码使用Configuration.FromAppConfig()方法加载多个组件。然后,它会在需要时按名称解析组件。单元测试工作正常。当我在我的MVC控制器中调用此代码时,我开始在对Configuration.FromAppConfig()的调用中出错。以下是一些示例代码:

这是一个简化版本,但说明了问题:

public interface IAuthorizer2
{
    string Url { get; set; }
}

public class Foo : IAuthorizer2
{
    public string Url { get; set; }
}

这是web.config中的组件:

  <component id="MessageManagerAdmins" service="MsmqManager.Areas.MessageManager.Controllers.IAuthorizer2"
type="MsmqManager.Areas.MessageManager.Controllers.Foo"/>

在我的控制器动作中,我打电话:

var container = new WindsorContainer();
container.Install(Configuration.FromAppConfig());

我得到以下异常:(我将完整性包括在内)

    Could not load file or assembly 'Spatial4n.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f9456e1ca16d45e' or one of its dependencies. The system cannot find the file specified.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.FileNotFoundException: Could not load file or assembly 'Spatial4n.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f9456e1ca16d45e' or one of its dependencies. The system cannot find the file specified.

Source Error: 


Line 100:        {
Line 101:            var container = new WindsorContainer();
Line 102:            container.Install(Configuration.FromAppConfig());

Source File: c:\Prj\AA\Source\Development\Web\MessageManager\Source\MessageManager\Areas\MessageManager\Controllers\MessageController.cs    Line: 102 

Assembly Load Trace: The following information can be helpful to determine why the assembly 'Spatial4n.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f9456e1ca16d45e' could not be loaded.


=== Pre-bind state information ===
LOG: User = ARCHSTONE\tstappaa
LOG: DisplayName = Spatial4n.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f9456e1ca16d45e
 (Fully-specified)
LOG: Appbase = file:///C:/Prj/AA/Source/Development/Web/AssociatePortal/Source/AssociatePortal/
LOG: Initial PrivatePath = C:\Prj\AA\Source\Development\Web\AssociatePortal\Source\AssociatePortal\bin
Calling assembly : Lucene.Net.Contrib.Spatial, Version=2.9.1.2, Culture=neutral, PublicKeyToken=85089178b9ac3181.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Prj\AA\Source\Development\Web\AssociatePortal\Source\AssociatePortal\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Spatial4n.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9f9456e1ca16d45e
LOG: The same bind was seen before, and was failed with hr = 0x80070002.

正在寻找Spatial4n.Core程序集。我以前从未听说过这个集会。我追溯到: 该网站使用RavenDB Raven.Database程序集引用Spatial4n.Core。

我可以理解为什么Castle会尝试解析因解析组件而可能调用的所有程序集。但是,如果你看一下我正在使用的接口/类的定义,这里没有任何依赖。我知道我可以下载Spatial4n.Core程序集的副本,以及所需的所有其他程序集,但我想了解如何正确配置容器以避免此问题。

我的问题:

  • 是否有更好的方法来填充容器/寄存器组件,以便我可以按类型和名称解析它们(即container.Resolve(“componentName”))
  • 有没有办法告诉Castle不解决所有可能的汇编参考?
  • 我在Castle Project网站和其他地方进行了很多搜索。我还没有找到有关Castle如何解析这些装配参考的信息。它主要是关于配置容器的更基本功能。您能否推荐更深入的文档,我可以从中了解更多相关信息?

感谢。

1 个答案:

答案 0 :(得分:1)

这不完全是温莎的行为,而是System.Reflection。加载程序集时,不会加载依赖项,但由于您使用的是“AllTypes”,这意味着每个类型(包括private)都将加载(通过反射),并提示加载程序解析依赖项。

避免这种情况的方法是使用显式注册Component.For&lt;&gt;等...

WRT按名称解析,默认情况下,完整的类型名称用作键(名称空间+类型名称)

文档很薄弱,但源代码并不太复杂。还有很多博客文章。

HTH