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