【发布时间】:2011-09-22 06:49:53
【问题描述】:
假设我有一个库 Lib.dll,它使用 Castle.Windsor 来初始化其服务。
我有一个主应用程序 App.exe,它使用反射在运行时加载 Lib.dll。 App.exe 事先并不知道 Lib.dll 的位置,只有在运行时才知道。
此时App.exe加载Lib.dll,Lib.dll初始化其服务时,会抛出System.TypeInitializationException异常,因为Castle.Windsor找不到服务类型。
Castle.MicroKernel.SubSystems.Conversion.ConverterException: Could not convert from 'Lib.TheServiceClass' to System.Type - Maybe type could not be found
at Castle.MicroKernel.SubSystems.Conversion.TypeNameConverter.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\TypeNameConverter.cs:line 91
at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion(String value, Type targetType) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:line 134
at Castle.MicroKernel.SubSystems.Conversion.DefaultConversionManager.PerformConversion[TTarget](String value) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\MicroKernel\SubSystems\Conversion\DefaultConversionManager.cs:line 162
at Castle.Windsor.Installer.DefaultComponentInstaller.SetUpComponents(IConfiguration[] configurations, IWindsorContainer container, IConversionManager converter) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\Installer\DefaultComponentInstaller.cs:line 196
at Castle.Windsor.Installer.DefaultComponentInstaller.SetUp(IWindsorContainer container, IConfigurationStore store) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\Installer\DefaultComponentInstaller.cs:line 52
at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers, DefaultComponentInstaller scope) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 327
at Castle.Windsor.WindsorContainer.Install(IWindsorInstaller[] installers) in e:\OSS.Code\Castle.Windsor\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 674
显然 Castle 找不到我的服务类,因为它位于不在 App.exe 目录中的 Lib.dll 中。当我将 Lib.dll 复制到 App.exe 目录时,问题就消失了,但必须复制它不是我们想要的。
那么我在 Lib.dll 中的代码如何告诉 Castle.Windsor 将类加载到正确的位置? (在 Lib.dll 位置而不是在 App.exe 位置)
【问题讨论】:
-
您使用的是哪个版本。容器中的组件是如何注册的?
-
@KrzysztofKoźmic 2.5.2 组件的注册方式是什么意思?我使用 XML 文件来定义服务
-
你能分享 XML 文件并展示你是如何加载它的吗?
-
@KrzysztofKoźmic
new WindsorContainer().Install(Configuration.FromXmlFile(configFile));也可以参考上面的堆栈跟踪 -
好的,我们正在慢慢实现。这段代码在哪里调用?
标签: c# .net reflection castle-windsor appdomain