Search code examples
castle-windsor

Installing a particular assembly first


I have multiple assemblies in my application, some of which contain installers. However, I need to ensure that the installers in my entry assembly are run first. (My application defines some decorators, and install order matters.)

I tried this:

_container.Install(FromAssembly.Containing<MyApplication>());
_container.Install(FromAssembly.InDirectory(new AssemblyFilter(applicationDirectory)));

but that runs the installers in my application assembly twice, causing a registration exception.

I can mitigate this by creating a field in the installers to track whether that installer has run, and return if it has, but that seems hacky.

Is there a proper way to have Windsor install a single assembly, then the rest of the assemblies in a directory without duplication?

I think what I'd like to do is something like this

_container.Install(FromAssembly.InDirectory(new AssemblyFilter(applicationDirectory))
                               .SkipAlreadyRegistered());

or

_container.Install(FromAssembly.InDirectory(new AssemblyFilter(applicationDirectory))
                               .Except(FromAssembly.Containing<MyApplication>()));

Solution

  • How about using InstallerFactory to alter the order?

    public class AppFirstFactory: InstallerFactory
    {
        public override IEnumerable<Type> Select(IEnumerable<Type> installerTypes)
        {
            return installerTypes.OrderBy(x => x.Assembly == GetType().Assembly ? 0 : 1);
        }
    }
    

    Then to register:

    _container.Install(FromAssembly.InDirectory(new AssemblyFilter(applicationDirectory), new AppFirstFactory()));