Search code examples
c#clrmef.net-assembly

Loading third party assembly referenced only by plugins with AssemblyCatalog


I'm using MEF in an Windows Azure Application where all assembly plugins are stored inside a blob storage. In the blob storage are stored not only the plugins but also all assemblies that are referenced by the plugins.

The code which load the plugins is the follows:

foreach (var pluginBytes in _pluginProvider.GetPlugins())
{
    var pluginAssembly = AppDomain.CurrentDomain.Load(pluginBytes);
    var assemblyCatalog = new AssemblyCatalog(pluginAssembly);
    catalog.Catalogs.Add(assemblyCatalog);
}

Unfortunately the plugins which reference a third part assembly will cause a ReflectionTypeLoadException when the ComposeParts method is called on the CompositionContainer.

Due to the cloud solution I cannot use a DirectoryCatalog. How can I solve this thorny problem?


Solution

  • If you have the third party assembly in another blob then you can subscribe to your application's domain AssemblyResolve event and use the arg.Name value to locate the third party assembly from the blob. Then load it the same way you load the plugins and return the reference of the loaded assembly.