I use MEF to map interface to implementation class as a way of DI. For example, I use the Import attribute for an interface, and Export for implementation class. My understanding is that the MEF framework will create the implementation class instances and hold them in MEF's container for use or auto-injection.
Some of my implementation classes implement IDispose interface. Since instances are created by MEF, I think I should let the MEF to call components' Dispose method if they are disposable when the MEF is out. For example, in my application, I hold a reference to the MEF's container. When the application terminates, I call the Dispose method of the container. The problem is that my components' Dispose is never called.
Here are some example codes about the import and export mapping:
[Import]
private IMyInterface IComponent1 { get; set; }
....
[Export]
private IMyInterface Component {
get {
var instance = new MyImplemetation();
....
return instance;
}
}
....
There many other import and export definitions for other mappings in the similar way. I construct mappings in this way so that the MEF has the knowledge of the relationships and the way how to create the mapped instances. Here are some codes in my application to load mappings by using AssemblyCatalog:
var catalog = new AggregateCatalog();
catalog.Add (new AssemblyCatalog(Assembly.GetExecutingAssembly());
var batch = new CompositionBatch();
batch.AddPart(catalog);
// MEF container has all the mappings
var container = new CompositionContainer(catalog);
....
// Get instance from container
var instance = container.GetExportedValue<IMyInterface>();
// my instance CTOR has a contructor with several other
// implementation instances injected by interface
// instance starts to do its job and coordinates others ...
instance.Start();
....
// Finally the job is done.
// Dispose the container explicitly there.
container.Dispose();
// But my components are never disposed
// this results some connections not being closed
// file streams not being closed...
Here the instance has many other components injected through CTOR by the MEF. Those components also contain other components which are injected by the MEF. The problem is that it is really hard to make decision when to dispose components since some instances are shared. If I called Dispose on one, this would cause others not being able to use it. As you can see in this picture, instances are created by the MEF and injected to my application classes. Each component should not have any knowledge of others, and it should use injected components to do the job.
I am not sure where/how I should instruct the MEF to call Dispose on components when the application terminates or the container is disposed? Should I call the Dispose on components? I don't think that is right since the MEF creates them and inject them into clients as required. The clients should not call their Dispose when finishing their jobs.