Search code examples
c#silverlightprismmef

Cannot getting object with MEF


I have three Assemblies

  • AssemblyX contains Interface IContext
  • AssemblyY contains class Context which implements IContext
  • AssemblyZ is the Client which uses Context and should register it to MEF, that IContext can be used from another Classes (in other Assemblies)

My Problem is now to register Context in MEF, that other instances can get it with IContext.

I have attached the Attribute [Export(typeof(IContext))] to Context. But when I now do this.Container.GetExportedValue<IContext>() I get a ImportCardinalityMismatchException.

The Constructor of Context is defined as follows

    [ImportingConstructor]
    public Context(IEventAggregator eventAggregator) { ... }

When I do this.Container.GetExportedValue<IEventAggregator>() for Testing to resolve the IEventAggregator, this is working.

Can somebody help me, what I am doing wrong? - Does I have to attach somewhere the AssemblyX and/or AssemblyY?

Thanks. Thomas


Solution

  • To resolve IContext(as Context from the assemblyY), the Container has to be aware of AssemblyY(that is, about all composable parts from it). In order to it, you should use the appropriate catalog. Or in case if AssemblyY is loaded on demand, it should be loaded before the Context is used.