Quantcast
Channel: Managed Extensibility Framework
Viewing all articles
Browse latest Browse all 265

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

$
0
0
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();
    }

Viewing all articles
Browse latest Browse all 265

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>