Reputation: 7183
I have a single class called Foo:
using System;
using System.ComponentModel.Composition;
namespace MefTest
{
[Export]
internal class Foo
{
public Foo()
{
Console.WriteLine("created foo");
}
~Foo()
{
Console.WriteLine("Dead");
}
}
}
It's created as such:
using System;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
namespace MefTest
{
internal class Program
{
public static void Main()
{
var catalog = new AggregateCatalog();
catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
var container = new CompositionContainer(catalog);
//EDIT: my problem, this returns Lazy<Foo> not Foo. Since I didn't call foo1.Value it was never actually created
var foo1 = container.GetExport<Foo>();
container.ReleaseExport(foo1);
foo1 = null;
GC.Collect();
Console.Read();
}
}
}
But it never seems to get disposed. I tried adding an IDisposible interface to it without any luck.
How can I ensure this gets cleaned up correctly? I thought that ReleaseExport would do it but the destructor never gets called, so it never appears to be cleaned up.
I've read http://mef.codeplex.com/wikipage?title=Parts%20Lifetime but I can't seem to see the problem with the above code.
Upvotes: 2
Views: 879
Reputation: 5465
The problem you have is that Foo
is a shared export. If you want it disposed as is, you can implement IDisposable
on it and then call Dispose
on the container.
The other option is to mark Foo as non-shared which will result in ReleaseExport calling the Dispose method on it.
[Export]
[PartCreationPolicy(CreationPolicy.NonShared)]
internal class Foo
{
public Foo()
{
Console.WriteLine("created foo");
}
~Foo()
{
Console.WriteLine("Dead");
}
}
The above is explained pretty well in the "Scoped operations and early reclaim of resources" section of the link you provided. Just remember that if you don't supply a PartCreationPolicy attribute, the export is shared by default.
Upvotes: 1