amateur amateur - 17 days ago 7
C# Question

Filter all assemblies in namespace except for current one

I am using IoC in my solution which is split into the following projects:

Toolbox.DAL

Toolbox.BL

Toolbox.EntityFramework

Toolbox.API (main project)


To register the components in my main project, I use the following bit of code:

using (WindsorContainer container = new WindsorContainer())
{
AssemblyFilter filter = new AssemblyFilter(HttpRuntime.BinDirectory, "Toolbox.*.dll");
IWindsorInstaller installer = FromAssembly.InDirectory(filter);

return new CastleInitialiser(container.Install(installer));
}


However, this is causing an issue where the dependencies in my main project are being registered twice - once during normal initialisation and then again from the compiled assembly.

Is there any way to change the filter so it will pick up all assemblies except the current project assembly? I have been searching around but could not find any decent examples of how to use the assembly filter properly.

Answer

Just looked at the source code

You can add additional filters.

using (WindsorContainer container = new WindsorContainer())
{
    AssemblyFilter filter = new AssemblyFilter(HttpRuntime.BinDirectory, "Toolbox.*.dll");

    //this
    filter.FilterByName(name => !name.Name.Equals("Toolbox.API"));

    IWindsorInstaller installer = FromAssembly.InDirectory(filter);

    return new CastleInitialiser(container.Install(installer));
}

My syntax might now be 100% correct, but it should illustrate how you can do it.

Comments