Cannot call SatisfyImports on a object of type 'xx' because it is marked with one or more ExportAttributes.

Apr 23, 2010 at 5:49 PM

Hi People,

I have application which shows widgets downloaded dynamically from web like this

var catalog1 = new DeploymentCatalog("FormSubmitter_Widget.xap" );
var aggregate = new AggregateCatalog (catalog1);
CompositionHost.Initialize(new DeploymentCatalog (), aggregate);
CompositionInitializer.SatisfyImports(this);

It works fine till I convert FormSubmitter_Widget into MEF type widget - basically my  exported widget is built from imported parts of type Country..

This is what I mean

[WidgetExport(typeof(IWidget))]
public partial class CountrieBrowser_Widget : UserControl, IWidget
{
   public CountrieBrowser_Widget(){
   InitializeComponent();
   CompositionInitializer.SatisfyImports(this);
}
[ImportMany]
public List<Country> Countries { get; set; }
}

Coutries classes have [export] attributes...

And this doesn't work. Error in constructor during SatisfyImports(this) call.

If I comment out CompositionInitializer.SatisfyImports(this) in  CountrieBrowser_Widget() constructor - no errors and no values in Countries property.

 

What I do wrong?

Thanks 

Apr 23, 2010 at 7:28 PM
Edited Apr 23, 2010 at 7:37 PM

You only need "CompositionInitializer.SatisfyImports(this);" if the object is not created by the MEF container. I'm not very familiar with silverlight but I assume this is the case for objects that are created by loading XAML. 

However, you also have "[WidgetExport(typeof(IWidget))]" on your class. The export attribute shows your intention to let the object be created by the MEF container.

The SatisfyImports error is telling you that you can either use SatisfyImports or an Export attribute. But using both doesn't make sense.

Apr 23, 2010 at 10:13 PM

ok. Agree.

Then how to initialaze properties of the object B via MEF if this object is being exported as well.

class A{
 public A(){CompositionInitializer.SatisfyImports(this);}
 [import]
 B PropertyA{get;set;}
}

[export]
class B{
public B(){CompositionInitializer.SatisfyImports(this);}
 [import]
 C PropertyB{get;set;}
}

[export]
class C{
blah blah..
}

I think constructor of class B should SatisfyImport and initialize PropertyB? Or constructor of class A should do all magic?

Apr 23, 2010 at 11:15 PM

Looks like I found bug.

I exported class C as [Export] and in class B PropertyB was empty...

If I export class C as [Export(typeof(someinterface)] everything is working fine.

Now question is why just [Export] doesn't do magic - import in class B should import all types B being exported.. -))

Brrr... I am confused.

Dec 22, 2012 at 11:42 AM

You only need to call  SatisfyImports once then use the "GetExportedValue" to new up the types from the container

  Container.GetExportedValue<ICountries>();



I know this is quite an old post but its nice to have questions wrapped up.