Difference between DirectoryCatalog and AssemblyCatalog

I have a MEF application with quite a lot of components in different dlls. When the program starts, I create an AggregateCatalog and then load all assemblies into it. Due to the large number of components, this can take some time and cause delays in loading the application. I would like to defer the loading of components until the end, but I am having a problem. When I load components with

this.AggregateCatalog.Catalogs.Add(new DirectoryCatalog("."));

      

I get about 125 components in the directory and everything works fine. When I load each dll and exe each time using AssemblyCatalog, I only get about 77 components and the application won't start due to missing components. I even created a directory listing and looped over it so that I don't miss any files and come up with the same result. My question is, what else does the DirectoryCatalog do that makes it work properly and how can I emulate it without loading everything into the directory all at once?

+3


source to share


1 answer


Difference between AssemblyCatalog and DirectoryCatalog

I downloaded the MEF source code from CodePlex ( link ). By examining DirectoryCatalog.cs, it creates Dictionary <string, AssemblyCatalog> () AssemblyCatalogs, one for each assembled assembly.

    this._assemblyCatalogs = new Dictionary<string, AssemblyCatalog>();
    this._catalogCollection = new ComposablePartCatalogCollection(null, null, null);

    this._loadedFiles = GetFiles().ToReadOnlyCollection();

    foreach (string file in this._loadedFiles)
    {
        AssemblyCatalog assemblyCatalog = null;
        assemblyCatalog = CreateAssemblyCatalogGuarded(file);

        if (assemblyCatalog != null)
        {
            this._assemblyCatalogs.Add(file, assemblyCatalog);
            this._catalogCollection.Add(assemblyCatalog);
        }
    }

      

As you can see here, it gets all assemblies from the specified directory using GetFiles () (which gets all files unless the optional search pattern is given). It then creates a new collection for each of these assemblies.

CreateAssemblyCatalogGuarded adds protection for exceptions.

So, as far as I can tell, there shouldn't be any difference between using the DirectoryCatalog (which would be my preferred way of doing it) and loading all assemblies in a specified directory with separate AssemblyCatalog (s), since that's exactly what DirectoryCatalog is all the same.



Is it possible that there is a bug in your code that creates a new AssemblyCatalog for each assembly?

I also looked at AssemblyCatalog which just loads the assembly and then gets all of its types.

Performance issues

I would like to defer the component loading part to a later version

If your speed problem is simply by creating your directories, the next bit won't help. Otherwise, if your performance issue occurs when composing your parts, instead of juggling yourself with AssemblyCatalogs, use the MEF Lazy Imports / Exports ( Another link ). When MEF normally creates parts, it creates an instance of all Importers and all child imports. If you have defined them as Lazy MEF will not instantiate these types / parts until needed (which saves time).

+2


source







All Articles