runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Implement MEF DirectoryCatalog

Open marcusien opened this issue 10 years ago • 7 comments

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 avatar May 06 '16 14:05 marcusien

@marcusien is there any workaround you can use for this?

AlexGhiondea avatar Nov 23 '16 22:11 AlexGhiondea

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 avatar Nov 28 '16 16:11 bradbart

@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.

AlexGhiondea avatar Nov 29 '16 18:11 AlexGhiondea

@bradbart how do you avoid the dependent assembly issue and System.IO.FileNotFoundException error when calling LoadFromAssemblyPath?

AceHack avatar Apr 24 '18 17:04 AceHack

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 avatar Jun 12 '18 15:06 bradbart

@bradbart load things that have dependencies or themselves in the nuget/package cache instead of the bin folder and you should see the issue.

AceHack avatar Jun 12 '18 16:06 AceHack

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.

bradbart avatar Jul 18 '18 21:07 bradbart