Resolver assembly file plug-ins in System.Composition (a la DirectoryCatalog)

Feb 8, 2014 at 11:09 AM
(I'll refer to System.ComponentModel.Composition as .net 4.5 MEF and System.Composition as Windows Store Apps MEF)

I am using System.Composition with Web Api.

Besides the referenced types in the host project I want to discover some contract enforcing plug-ins in a folder.

How can I accomplish it with System.Composition?

Do I need to bridge it with System.ComponentModel.Composition? And If so will it hurt my performance?

I have used System.Composition since it seems a lightweight version for Windows Store and Web Apps. Should I just stick with System.ComponentModel.Composition?

Thanks.
Feb 8, 2014 at 11:46 AM
Would loading the assembly from file system when bootstrapping, thus feeding it to the ContainerConfiguration.WithAssembly(...) be an acceptable solution? Is there a more elegant solution using MEF's facilities?
Nov 17, 2014 at 4:03 PM
Hi,
I have the same issue.
Trying to implement kind of plug in architecure using lightweight mef (for wp8.1), and i wonder what is the way to implement DirectoryCatalog with System.Composition?

I want to enable external users to export their own plug ins (of course, based on my interface) and load them dynamiclly, but i can't find a simple way to do this using the ComposistionHost.

Can you please advise?
Thanks!
Jan 15, 2015 at 1:31 PM
There's much confusion regarding the DirectoryCatalog that can't be referenced from System.ComponentModel.Composition in VNext ASP.NET projects. It's just a case of different method of doing the same thing:

using System.IO;
using System.Composition;
using System.Composition.Hosting;
using System.Reflection;
    public class Imports
    {
        [ImportMany]
        public IEnumerable<Lazy<IPlugin>> Plugins { get; set; }

        public Imports()
        {
            var configuration = new ContainerConfiguration()
                   .WithAssembly(typeof(IPlugin).GetTypeInfo().Assembly);

            var compositionHost = configuration.CreateContainer();         
            compositionHost.SatisfyImports(this);                                          
        }                

        public Imports(string pluginPath, string searchPattern)
        {
            var dirCat = new List<Assembly>();
            var dirInfo = new DirectoryInfo(pluginPath);
            var dirDlls = dirInfo.GetFiles(searchPattern, SearchOption.TopDirectoryOnly);
            foreach (var fileInfo in dirDlls)
            {
                var dllPath = fileInfo.FullName;
                var newAssembly = Assembly.Load(System.IO.File.ReadAllBytes(dllPath));
                dirCat.Add(newAssembly);
            }
            var configuration = new ContainerConfiguration()
                   .WithAssemblies(dirCat);
            var compositionHost = configuration.CreateContainer();
            compositionHost.SatisfyImports(this);
        }
    }

   static void Main(string[] args)
    {

        Console.WriteLine("START ===>");
        var dirPath = @"C:\jedev\ConsoleApplication1\ConsoleApplication1\bin\Debug";
        var i = new Imports(dirPath, "*.dll");
        foreach( var p in i.Plugins)
        {
            Console.WriteLine(p.Value.PluginName);
        }

        Console.WriteLine("END ===>");
        Console.ReadLine();
    }