如何在使用反射加载的程序集中使用 Castle.Windsor [英] How to use Castle.Windsor in an assembly loaded using reflection

查看:27
本文介绍了如何在使用反射加载的程序集中使用 Castle.Windsor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个库 Lib.dll,它使用 Castle.Windsor 来初始化其服务.

Let's say I have a library Lib.dll, which uses Castle.Windsor to initialize its services.

我有一个主应用程序 App.exe,它使用反射在运行时加载 Lib.dll.App.exe 事先不知道 Lib.dll 的位置,它只在运行时知道.

I have a main application App.exe, which loads Lib.dll on runtime using reflection. App.exe does not know the location of Lib.dll beforehand, it is only known at runtime.

在这种情况下,当App.exe加载Lib.dll和Lib.dll初始化其服务时,会抛出System.TypeInitializationException异常,因为Castle.Windsor找不到服务类型.

In this case, when App.exe loads Lib.dll and Lib.dll initialize its services, a System.TypeInitializationException exception is thrown, because Castle.Windsor cannot find the service type.

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 找不到我的服务类,因为它位于 Lib.dll 中,但不在 App.exe 的目录中.当我将 Lib.dll 复制到 App.exe 目录时,问题就消失了,但不必复制这不是我们想要的.

Apparently Castle cannot find my service class because it is in Lib.dll that is not located in App.exe's directory. When I copy Lib.dll to App.exe directory, the problem goes away, but having to copy this is not something we want.

那么我在 Lib.dll 中的代码如何告诉 Castle.Windsor 将类加载到正确的位置?(在 Lib.dll 位置而不是在 App.exe 位置)

So how can my code in Lib.dll tell Castle.Windsor to load the class in the correct location? (in Lib.dll location instead of in App.exe location)

推荐答案

您可以尝试通过 AssemblyResolve 事件在代码中加载未解析的程序集

You can try to load the unresolved assemblies within your code by AssemblyResolve event

AppDomain.CurrentDomain.AssemblyResolve += (sender, args) =>
{
     string typeToLoad = args.Name;
     string myPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
     return Assembly.LoadFile(...); //or return Assembly.GetExecutingAssembly() etc.
};

这篇关于如何在使用反射加载的程序集中使用 Castle.Windsor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆