0

Again the topic of MEF not loading plugins correctly..

First of all: I took the last 7 hours searching similar questions and trying to apply the solutions given there, but I couldn't find any solution that worked for me.

Here is the Situation: Main application using 3 classes

  • Main class with GUI ( Windows forms )
  • Export class with Interface IMain for the PLugIns being able to give messages to the main window
  • Import class that loads all available Plugins from a given Directory

All this worked like a charm for one directory, where all the Plugins were stored. Now my superiors wanted me to rework it, so that every Plugin gets its own subdirectory within the main plugin Directory. And that's were the trouble began. I tried modifying my Code to get this Task done but now all the Plugins are loaded into the Catalogs but are missing in the list, from where I can call them through the applications GUI.

Here the code snippet from my Import class:

public class ServiceTool
    {
        CompositionContainer ComContainer;
        [ImportMany(typeof(Interfaces.IPlugIn))]
        public List<Interfaces.IPlugIn> Liste = new List<Interfaces.IPlugIn>();

        public ServiceTool()
        {
            ModuleSuchen();
            MessageBox.Show(Liste.Count.ToString());
        }

        void ModuleSuchen()
        {
            var AggKatalog = new AggregateCatalog();
            var Dlls = Directory.GetFiles("D:\\Automation\\TIA\\FER-ServiceTool\\PlugIns", "*.dll", SearchOption.AllDirectories);
            foreach(var DllDatei in Dlls)
            {
                try
                {
                    AggKatalog.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFile(DllDatei)));
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.ToString());
                }
            }
            ComContainer = new CompositionContainer(AggKatalog);
            try 
            {
                this.ComContainer.ComposeParts(this);
            } 
            catch (CompositionException ComEx) 
            {
                var ComExceptions = ComEx.Errors;
                foreach (var ComException in ComExceptions) 
                {
                    MessageBox.Show(ComException.ToString());
                }
            } 
            catch (ReflectionTypeLoadException RTLEx) 
            {
                Exception[] Exceptions = RTLEx.LoaderExceptions;
                foreach (Exception Ex in Exceptions) 
                {
                    MessageBox.Show(Ex.ToString());                     
                }
            }
        }
    }

Where is the problem loading the plugins from the subdirectories? I'm just a beginner in programming and I have absolutely no clue why it isn't working anymore..

Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • Does this answer your question? [MEF recursive plugin search](https://stackoverflow.com/questions/8138867/mef-recursive-plugin-search) – dymanoid Dec 16 '19 at 14:34
  • maybe by describing the actual problem instead of *why* you want it to work could be more effective.`this worked like a charm for one directory` did you try to find out what was the differences that made it work back then? – Bizhan Dec 16 '19 at 14:36
  • @dymanoid this was one of the Solutions I actually tried but unfortunately it didn't work. The Code I posted was only the latest Version I tried. – Raketenmaulwurf Dec 16 '19 at 14:42
  • @Bizhan The only difference as I said is the Directory.GetFiles now searching AllDirectories instead of TopDirectoryOnly. I just changed it from ONE Folder to ONE Folder plus subdirectories – Raketenmaulwurf Dec 16 '19 at 14:45
  • did you try to add the directory as well? `AggKatalog.Catalogs.Add(new DirectoryCatalog(DllDatei.Directory));` – Bizhan Dec 16 '19 at 15:10
  • GetFiles Returns an Array of strings and the strings have no .Directory method so my compiler doesn't accept this. I will try to enumerate the dlls as Files or FileInfos, Maybe it'll work – Raketenmaulwurf Dec 16 '19 at 15:20

0 Answers0