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.
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
/ Contract / Exportmetadata
var partTypeName =
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
- and ReflectionModelServices.CreateImportDefinition are the significant ones.
Register an EventHandler for
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.