I've got a very simple app with a single export and multiple imports for the same type.
After I call ComposeParts I can see that the import worked in the same class where I called ComposeParts from - the MyService property has been hooked up.
The problem is that I have another UserControl that needs access to MyService and the property isn't set - it's in the same package etc. but it's not instatiated at the time I call ComposeParts.
If I make my CompositionContainer public / static and call ComposeParts and pass the UserControl the MyService property is set but that's a horrible solution.
Can anyone shed some light on what's going on? Is ComposeParts only smart enough to hook up existing objects or will an Import attribute be able to work on objects at a later time? Did I hook something up incorrectly?
public partial class App : Application
{
protected override void OnActivated(EventArgs e)
{
AssemblyCatalog assemblyCatalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
compositionContainer = new CompositionContainer(assemblyCatalog);
compositionContainer.ComposeParts(this);
}
[Import(typeof(MyService))]
public MyService MyService { get; set; }
}
Update:
I'm trying to upgrade a 250k line C# prject from the Service Provider Model found in .Net 2.0 to MEF.
It doesn't seem like you can have a new object instance automatically be hooked up to the services it requires solely through the Import attribute. It seems like you need to re-trigger the ComposeParts or something like that. Lame.
In the .Net 2.0 provider/container model you needed to explicitly add child objects to parent containers and finding a service would be a recursive check from children to parents containers. I'm not sure what the corrolary is in MEF??