Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>MEF does manage the lifetime of the components it creates. It looks like the problem in your example is that the object you want disposed is not actually created by MEF. Perhaps you want to do something like this:</p> <pre><code>public class ComponentExporter : IDisposable { private IMyInterface _component; [Export] public IMyInterface Component { get { if (_component != null) { _component = new MyImplementation(); // ... } return _component; } } public void Dispose() { if (_component != null) { _component.Dispose(); } } } </code></pre> <p>ComponentExporter is the class actually created by MEF, and if it implements IDisposable then MEF will dispose it with the container. In this example ComponentExporter disposes the created component when it is dispose, which is likely what you want.</p> <p>Of course it would be easier if you just put the export on the MyImplementation class directly. I assume you have some reason for not doing that, but this is how it would look:</p> <pre><code>[Export(typeof(IMyInterface))] public class MyImplementation : IMyInterface, IDisposable { // ... } </code></pre> <p>A few other notes on your code: You probably don't need to add the catalog to the container via the batch, unless you are importing it somewhere and modifying it from parts inside the container. And if you happen to be processing many requests and are concerned about performance, you should only create the AssemblyCatalog once, and then use the same one for all requests.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload