Dispose of Exported Objects

Dec 18, 2008 at 9:08 PM
In the previous release Dispose() would be called on exported objects, which implemented IDisposable, when the composition container was disposed.  This no longer seems to be the case for the current release.  Is this a bug with the current build or has this behavior been removed?
Dec 19, 2008 at 6:42 PM
Dispose is called on the ComposableParts. Our AttributedComposablePart will call dispose if it happens to hold a disposable instance. Note that we never dispose the exports, only the part that expose them.

I am not aware of any change of behavior related to disposable types. Can you share your scenario or a sample code that illustrates the problem?

Jan 10, 2009 at 3:22 AM
Below is a program which illustrates the issue.  With preview 2 Dispose would be called on the export and with preview 3 it does not.  It would be difficult to manually call Dispose on particular imports especially when a collection is imported and Dispose should only be called on items that were instanced.

using System;
using System.Diagnostics;
using System.ComponentModel.Composition;

namespace TestMEFDispose
    public class DisposableExport : IDisposable
        #region IDisposable Members

        public void Dispose()
            Debug.WriteLine("Dispose called");


    class Program
        static void Main(string[] args)
            var catalog = new AggregatingComposablePartCatalog();
            catalog.Catalogs.Add(new AttributedAssemblyPartCatalog(typeof(Program).Assembly));

            CompositionContainer container = new CompositionContainer(catalog.CreateResolver());
            CompositionContainer container = new CompositionContainer(catalog);

            DisposableExport export = container.GetExportedObject<DisposableExport>();


Jan 10, 2009 at 8:00 PM
It looks like you are correct. There was a bug in preview 3 where we didn't dispose those exports/parts. We have done some lifetime work since preview 3 and so this is fixed internally right now and it will show up in the next preview.

Thanks for the report.