Can Castle Windsor find files in a subdirectory?

I have a rich client application that uses Castle Windsor. At the moment, all assemblies, including the exe app, are in the same folder, but it all looks pretty messy. I would like to put my dlls inside a subfolder like "bin", but this prevents Castle etc. from being searched. When you call. In fact, the application starts at startup.

Is there a way to tell Castle to look elsewhere for files?

+1


source to share


3 answers


It depends on how you set up Windsor.



If you are using the Castle.MicroKernel.Registration interface, you have to load assemblies manually and then register the loaded types with the container.

+1


source


The lock does not load the assembly itself, it just relies on merge to find the assembly based on its default behavior - so this is a more general .Net Framework question.

One way to do what you want is to deal with the assembly resolution failure and direct the runtime to where the assembly might be located - one way to achieve this is to override the assembly solution (see msdn ) and then write some code to find and download the appropriate assembly from the correct location.



This will obviously allow you to support any directory scheme, not just a binary subdirectory (so you can, for example, have a separate directory for plugins).

+1


source


You can use a custom XmlInterpreter to initialize your container. Create a class that inherits XmlInterpreter and puts the following override in that class: This process processes all * .dll.config in the current executing assembly directory, which can easily be rewritten to use recursive file search.

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 );
        }
    }
}

      

0


source







All Articles