Table of Contents

Authoring MEF parts

Disposing MEF parts

.NET MEF and NuGet MEF recognize parts that implement IDisposable. Support for IAsyncDisposable parts is unique to VS MEF. Implement IAsyncDisposable when cleanup requires asynchronous work instead of starting unobserved work from Dispose.

VS MEF disposes an instantiated part when its owning lifetime ends. The owning lifetime is usually the ExportProvider, but a non-shared part created by an ExportFactory<T> is owned by the export lifetime returned from CreateExport. Dispose that export lifetime promptly when the part is no longer needed.

When a lifetime is disposed asynchronously, VS MEF calls DisposeAsync and awaits it. If a part implements both IDisposable and IAsyncDisposable, asynchronous disposal calls only DisposeAsync. Synchronous lifetime disposal also cleans up parts that implement only IAsyncDisposable by blocking until DisposeAsync completes.

See Disposing an ExportProvider for host configuration and disposal guidance.