How to use import and export information without keeping the Type loaded

Sep 27, 2012 at 5:19 AM

Hi all

I'm working on an app that will separate using a plugin from detecting and selecting the plugin for that I'm trying to figure out how to get import and export information from MEF without keeping the Type that defines the import / export loaded (so that I can unload the DLL), then use that information and later on reload the correct type and connect it with MEF. For a more complete explaination see the following two StackOverflow questions.

If anybody can give me some pointers that would be very helpful

Thanks

Petrik

Sep 28, 2012 at 8:19 PM

The scenario is actually a neat one, and it is well supported by in the box MEF.   If I understand correctly Petrik wants to do composition on the client and only download to the client only the required plugins, as opposed to downloading all possible plugins.  The way he can do that is to perform discover on the host, serialize the results to a file, and then distribute the file to the clients.  Use the file to construct a catalog and then when actually composing using the type he would download the required assembly.

 There are three components required. 

 

1)      A discovery / serialization mechanism. (possibly compile time, possibly Host based) …. This is not hard to do, but it is quite involved.

  • ·         First create a catalog using AssemblyCatalog or TypeCatalog
  • ·         Then enumerate through the composable part definitions
  • ·         For each composable part definition (Note: Types can be generics )

    ·    Extract the Part Data / the Part Metadata

    ·        Exports / Contract / Exportmetadata

    ·        Imports / ImportMetadata

Eg.

                var partTypeName = ReflectionModelServices.GetPartType(cpd).Value.AssemblyQualifiedName;

 2)      Create a Catalog and implement GetEnumerator (Parts property on V1.0 MEF) For the client

  • ·         The implementation of get enumerator should read the serialized data and use ReflectionModelServices APIs to re-construct a ComposablePartDefinition from the raw data
    • ReflectionModelServices.CreatePartDefinition,
    • ReflectionModelServices.CreateExportDefinition
    • and ReflectionModelServices.CreateImportDefinition are the significant ones.

 3)      Register an EventHandler for AppDomain.CurrentDomain.AssemblyResolve.

  • ·         This needs to notice when the CLR could not find the assembly (which will first occur when trying to satisfy an export from this catalog) The code will need to then go back to the server and download the needed assembly, and load it into memory.

None of the code you need to write is particulary gnarly, but you will need to take into consideration open generics and ExportFactories.  Good luck it seems like a fun project.

 Kevin 

Oct 3, 2012 at 10:45 PM

Hi Kevin

Thank you very much for the pointers. I have implemented the discovery / serialization part of the code and I will get started on the other bits shortly. I do have a few more questions though.

  • Can you explain a bit more about the ExportFactories? I have managed to get the open generics parts to work (as far as I can tell) but I'm still confused about the ExportFactories. Are those exports of methods and properties or is that something different entirely?
  • Is there a known (and available) test set that I can use to test my discovery / serialization / deserialization / instantiation implementation? I have written some of my own test cases but I suspect I'm missing some of the more tricky cases.
  • Is there a way to split the selection of the desired imports and exports from the actual composition? Would it be possible to let the host decide which parts should be created and connected, and then do the actual instantiating of the parts and composing them on the client? Say for instance by letting the host handle the outline of the composition, writing that to a stream / file / etc. and sending that to the client. The client then only has to acquire the correct assemblies and execute the composition outline in order to load and connect the right parts.

Thanks

Petrik

Oct 4, 2012 at 6:35 AM

Also if you want to get the SO credits then can you please post an answer to the SO question. In that case I'll mark it as the answer. Otherwise I'll answer it myself and point people to this discussion (and obviously I'll give you the credits for the answer).

Thanks

Petrik