I have a rich client application that uses Castle Windsor. At the moment all the assemblies including the application exe are in the one folder but it all looks rather untidy. I would like to put my dlls inside a subfolder such as "bin" but this prevents Castle from locating types etc when called upon. In fact the app crashes at start up.
我有一个使用Castle Windsor的富客户端应用程序。目前包括应用程序exe的所有程序集都在一个文件夹中,但它看起来都不整洁。我想将我的dll放在像“bin”这样的子文件夹中,但这会阻止Castle在调用时定位类型等。实际上应用程序在启动时崩溃了。
Is there a way to tell Castle to look somewhere else for the files?
有没有办法让Castle看到其他地方的文件?
3 个解决方案
#1
1
That depends on how you are configuring the Windsor.
这取决于您如何配置Windsor。
In case you use Castle.MicroKernel.Registration interface, you should load assemblies manually and then register loaded types in the container.
如果您使用Castle.MicroKernel.Registration接口,则应手动加载程序集,然后在容器中注册已加载的类型。
#2
1
Castle doesn't do any assembly loading itself, it just relies on fusion to locate the assembly based on it's default probing behaviour - so this is more a general .Net Framework question.
Castle本身不进行任何程序集加载,它只依赖于融合来根据它的默认探测行为定位程序集 - 所以这更像是一个通用的.Net Framework问题。
One way to do what you want is to handle the assembly resolution failure and directing the runtime to where the assembly can be located - one way to achieve this is to override assembly resolve (see msdn for details) and then write some code to locate and load the appropriate assembly from the correct location.
执行所需操作的一种方法是处理程序集解析失败并将运行时指向程序集所在的位置 - 实现此目的的一种方法是覆盖程序集解析(有关详细信息,请参阅msdn),然后编写一些代码以查找和从正确的位置加载适当的组件。
This would obviously allow you to support any kind of directory scheme, not just a binary subdirectory (so you could for instance have a separate directory for plugins).
这显然允许您支持任何类型的目录方案,而不仅仅是二进制子目录(因此您可以为插件提供单独的目录)。
#3
0
You can use a custom XmlInterpreter to initialize your container with. Create a class that inherits XmlInterpreter and put the following override in that class: This one processes all *.dll.config in the directory of the current executing assembly, which easily could be rewritten to use a recursive file lookup of some kind.
您可以使用自定义XmlInterpreter来初始化容器。创建一个继承XmlInterpreter的类,并在该类中放置以下覆盖:这个处理当前正在执行的程序集的目录中的所有* .dll.config,可以轻松地重写它以使用某种类型的递归文件查找。
public override void ProcessResource( Castle.Core.Resource.IResource source, Castle.MicroKernel.IConfigurationStore store )
{
base.ProcessResource( source, store );
var baseDir = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location );
foreach( var extraConfig in Directory.GetFiles( baseDir, "*.dll.config" ) )
{
try
{
var interpreter = new XmlInterpreter( extraConfig ) { Kernel = Kernel };
interpreter.ProcessResource( interpreter.Source, store );
}
catch(ConfigurationErrorsException)
{
throw;
}
catch( Exception ex )
{
throw new InvalidOperationException( "Failed to load configuration: " + extraConfig, ex );
}
}
}
#1
1
That depends on how you are configuring the Windsor.
这取决于您如何配置Windsor。
In case you use Castle.MicroKernel.Registration interface, you should load assemblies manually and then register loaded types in the container.
如果您使用Castle.MicroKernel.Registration接口,则应手动加载程序集,然后在容器中注册已加载的类型。
#2
1
Castle doesn't do any assembly loading itself, it just relies on fusion to locate the assembly based on it's default probing behaviour - so this is more a general .Net Framework question.
Castle本身不进行任何程序集加载,它只依赖于融合来根据它的默认探测行为定位程序集 - 所以这更像是一个通用的.Net Framework问题。
One way to do what you want is to handle the assembly resolution failure and directing the runtime to where the assembly can be located - one way to achieve this is to override assembly resolve (see msdn for details) and then write some code to locate and load the appropriate assembly from the correct location.
执行所需操作的一种方法是处理程序集解析失败并将运行时指向程序集所在的位置 - 实现此目的的一种方法是覆盖程序集解析(有关详细信息,请参阅msdn),然后编写一些代码以查找和从正确的位置加载适当的组件。
This would obviously allow you to support any kind of directory scheme, not just a binary subdirectory (so you could for instance have a separate directory for plugins).
这显然允许您支持任何类型的目录方案,而不仅仅是二进制子目录(因此您可以为插件提供单独的目录)。
#3
0
You can use a custom XmlInterpreter to initialize your container with. Create a class that inherits XmlInterpreter and put the following override in that class: This one processes all *.dll.config in the directory of the current executing assembly, which easily could be rewritten to use a recursive file lookup of some kind.
您可以使用自定义XmlInterpreter来初始化容器。创建一个继承XmlInterpreter的类,并在该类中放置以下覆盖:这个处理当前正在执行的程序集的目录中的所有* .dll.config,可以轻松地重写它以使用某种类型的递归文件查找。
public override void ProcessResource( Castle.Core.Resource.IResource source, Castle.MicroKernel.IConfigurationStore store )
{
base.ProcessResource( source, store );
var baseDir = Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location );
foreach( var extraConfig in Directory.GetFiles( baseDir, "*.dll.config" ) )
{
try
{
var interpreter = new XmlInterpreter( extraConfig ) { Kernel = Kernel };
interpreter.ProcessResource( interpreter.Source, store );
}
catch(ConfigurationErrorsException)
{
throw;
}
catch( Exception ex )
{
throw new InvalidOperationException( "Failed to load configuration: " + extraConfig, ex );
}
}
}