When you ask an ExportFactory
to create a new object, MEF might actually also create dependencies, and the dependencies of those dependencies, etcetera. Many objects might be created because you asked for a single object.
Some of these extra objects might be IDisposable
, and the container is responsible for disposing those when they are no longer necessary.
To signal to the container that you are done using your object, you call ExportLifetimeContext<T>.Dispose()
. The MEF container will then take care of disposing the requested object and its dependencies if necessary. If you don't do this, MEF will keep references to these objects forever, waiting for a signal that it can dispose them.
In Autofac 2, a very similar mechanism exists. Instead of ExportFactory
, they use Func<Owned<T>>
and you need to call Owned<T>.Dispose()
to clean up.
edit: The MEF documentation has a
Parts lifetime section where it is described in which cases exactly the container keeps references to exports. It does not yet mention
ExportLifetimeContext.Dispose
but I imagine this is similar to
CompositionContainer.ReleaseExport
.
edit: note that
ExportFactory
is intended for cases where you have a clearly defined lifetime. If this is not the case (or you know that clean-up is never necessary), then you should create your own factory implementation. Of course, it is then your responsibility to make sure that nothing
IDisposable
is created, since it would be impossible to clean it up properly.
Here is an example of a custom factory import/export using Func<T>
.
[Export(typeof(ISomeInterface))]
public class SomeClass
{
private readonly Func<Foo> fooFactory;
[ImportingConstructor]
public SomeClass(Func<Foo> fooFactory)
{
this.fooFactory = fooFactory;
}
public void DoStuff()
{
Foo newFoo = fooFactory();
...
}
}
public class FooFactory
{
[Export(typeof(Func<Foo>))]
public void CreateFoo()
{
...
}
}