Search code examples
.netc#-4.0mefremoting

How to create a custom ComposablePart that satisfies imports in a remote AppDomain?


I have created a custom ComposablePartCatalog, ComposablePartDefinition, and ComposablePart.

These all work together so that the composed part is created in a separate AppDomain. All of which are working. The problem is when an instance of an exported type that includes an import, i.e. a field has the Import attribute, gets created, that field is not being set to the expected imported type, i.e. it is null.

When I don't use my custom ComposablePartDefinition, i.e. let it do the default composition, the imports are created so it is not anything related to the types. So it must be something I'm missing in my custom Composable classes.

I am not an expert at this and I have been searching the web for any information on creating a custom ComposablePart which satisfies imports but can find any information on how to do this correctly.

In my custom ComposablePart, I override GetExportedValue which returns the instance of the exported type and this works fine as long as the exported types don't import anything.

I am obviously missing something important but I cannot find the documentation that explains how the imports are created.

In the ComposablePart, there is the SetImport override, but how am I supposed to implement this in my derived class? Is this the key to get my imports working? Or do I need to do something in GetExportedValue when I actually create the instance of the exported type to get it to "compose" its imports?


Solution

  • I have figured it out on my own. In the context of the "remote" AppDomain where the remote instance is created, I need to compose the remote instance using a new catalog (which I create using information passed from the calling domain) and a new CompositionContainer passing the remote instance into the constructor.