MEF/SL4/CompositionException/GetExportedValue

Nov 3, 2010 at 9:30 AM

In a Silverlight 4 project using MEF for on-demand loading of XAP files, I have a strange effect appeared today for the first time.

As soon as I call the container's GetExporedValue<T>() method for retrieving (!) the exported instance of an object implementing this interface, I get a composition exception saying that there is already an element added with the same key (yes, that's why I want to retrieve it!). When I remove the [Export] from my class, the exception is what I would have expected: No exported part found. If have no idea why this happens. If somebody has an idea, please let me know. Thanks!

  StackTrace:
       System.ComponentModel.Composition.Hosting.CompositionServices.GetExportedValueFromComposedPart(ImportEngine engine, ComposablePart part, ExportDefinition definition)
       System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportedValue(ComposablePart part, ExportDefinition export, Boolean isSharedPart)
       System.ComponentModel.Composition.Hosting.CatalogExportProvider.CatalogExport.GetExportedValueCore()
       System.ComponentModel.Composition.Primitives.Export.get_Value()
       System.ComponentModel.Composition.ExportServices.GetCastedExportedValue[T](Export export)
       System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValueCore[T](String contractName, ImportCardinality cardinality)
       System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValue[T](String contractName)
       System.ComponentModel.Composition.Hosting.ExportProvider.GetExportedValue[T]()

Nov 3, 2010 at 9:36 AM

Let's give some additional infos:

SL4-Code-Library:

[Export(typeof(IMyInterface)]
public class MyClass : IMyInterface
{
}

Another SL4-Assembly (has a reference to the above library)
public class MyClass2
{
    Container.GetExportedValue<IMyInterface>(); // causes the error to occur
}

The assemblies are compiled into a XAP file that is loaded on demand. That code worked for weeks, but suddenly not any more. I don't know yet what has changed ...

 

 

 

 

Nov 3, 2010 at 10:04 AM

Problem solved. It was not a 'MEF-problem'. I made a mistake in a configuration file what lead to a object instanciation problem which finally let MEF fail...