Best ExcludeAssemblies method to exclude all dlls except my dlls.

Aug 20, 2012 at 11:47 AM
Edited Aug 20, 2012 at 11:48 AM

I have a section of code to allow me to resolve jobs from all dlls in a particular folder , something like -

 

 DirectoryInfo di  = ...
 var assemblyNames = di.GetFiles("*.dll", SearchOption.AllDirectories).Select(f => f.FullName)).ToList();

   container = new UnityContainer();
   container
     .ConfigureAutoRegistration()
     .ExcludeSystemAssemblies()
     .LoadAssembliesFrom(assemblyNames)
     .Include(If.Implements<MyJob>, Then.Register().UsingPerCallMode())
     .ApplyAutoRegistration();

 

This works fine and at runtime this process takes a fraction of a second. However if I am debugging from inside visual studio, it takes at least 10 seconds and sometimes much longer.

This appears to be because the autoregistration process is checking through every type in every dll that Visual Studio has loaded, including quite a few relating to Visual Studio itself and also third party components such as DevExpress which also happen to be loaded.

Because this code runs at startup, it is annoying having this unnecessary delay.

So I was tried to write an ExcludeAssemblies method that will ignore everything except those dlls in assemblyNames.

My first attempt was 

   .ExcludeAssemblies(r => !assemblyNames.Contains(r.Location))

but this turns out to be unreliable, because sometimes r.Location can be null. 

Does anyone know the best way to do this?

Coordinator
Aug 23, 2012 at 10:37 AM
Edited Aug 23, 2012 at 10:38 AM

What about 

.ExcludeAssemblies(r => !assemblyNames.Select(p => Path.GetFileNameWithoutExtension(p)).Contains(r.GetName().Name))
Aug 24, 2012 at 8:40 PM

Thanks. That seems to work ok.