Implement MEF DirectoryCatalog
Hi,
Really need to got back the feature of MEF to get all dlls from a directory via DirectoryCatalog. Is there something done in that way ?
Thank you
@marcusien is there any workaround you can use for this?
I found the following code snippet is functionality equivalent to the usage of directory catalogs:
// Find and load all the DLLs in the folder
var assemblies = Directory.GetFiles(directory, "*.dll")
.Select(path => AssemblyLoadContext.Default.LoadFromAssemblyPath(path))
.Where(x => x != null);
// Add the loaded assemblies to the container
var configuration = new ContainerConfiguration()
.WithAssemblies(assemblies)
.WithAssembly(this.GetType().GetTypeInfo().Assembly);
return configuration.CreateContainer();
I think this approach works well, though it would be nice to have an extension method on the ConfigurationContainer that does this. So instead of WithAssemblies, use WithAssembliesFromDirectory() which loads the assemblies and adds them to them to the configuration.
@bradbart thanks for adding the workaround to the issue.
Since we don't currently have any plans to bring back more APIs for MEF and since we have an acceptable workaround I am closing this.
@bradbart how do you avoid the dependent assembly issue and System.IO.FileNotFoundException error when calling LoadFromAssemblyPath?
Hi @AceHack can elaborate on the nature of the issue you are encountering? I didn't notice any IO Exceptions occur when I ran this in my project.
@bradbart load things that have dependencies or themselves in the nuget/package cache instead of the bin folder and you should see the issue.
I suspect that this issue is caused by the application not having permissions to the directory outside of its bin. Doing a ReadFile from that directory should confirm whether that is the issue. Also, might be worth double checking the path if using a relative reference to the directory. Outside of that, I'm not sure why that exception would occur.