Is it possible to remove ComposablePartDefinitions one by one?

May 23, 2012 at 8:14 AM
Hi,

i have written a custom ComposablePartCatalog which extends the AggregagateCatalog and implements the INotifyComposablePartCatalogChanged Interface.

In that catalog exists a method Recompose, that i have found in some forum, with which it is possible to remove/add ComposablePartDefinitions from/to the Composition.

private void Recompose()
        {
            using (var atomicComposition = new AtomicComposition())
            {
                try
                {
                    if (Changing != null)
                    {
                        Changing(
                            this,
                            new ComposablePartCatalogChangeEventArgs(
                                this.myAddDefinitions.AsReadOnly(),
                                this.myRemoveDefinitions.AsReadOnly(),
                                atomicComposition));
                    }
                }
                catch (ChangeRejectedException e)
                {
                }

                foreach (var removedPart in this.myRemoveDefinitions)
                {
                    this.myComposablePartDefinitions.Remove(removedPart);
                }

                atomicComposition.Complete();
            }

            if (Changed != null)
            {
                Changed(
                    this,
                    new ComposablePartCatalogChangeEventArgs(
                        this.myAddDefinitions.AsReadOnly(),                        this.myRemoveDefinitions.AsReadOnly(),
                        null));
            }
        }

This works well if i remove Definitions only once.

But if i, for example, remove one Definition (Definition1) and then, in a second call, another Definition (Definition2), the first Defintion (Definition1) is back in the Composition and used.

I've tracked the problem down to "CatalogExportProvider" in the MEF, which contains a Collection called "_activatedParts" holding all the activated Exports.

Now when I fire "Changing"-Event, the "OnCatalogChanging"-Mehtod of CatalogExportProvider is called, in which the "ComposablePartDefinition" that should be removed, gets removed from the "_activatedParts" Collection. But after that the "DetermineRejection"-Method gets called, in which the Part is added again to the "_activatedParts" Collection and therefore will be used again if I remove another "ComposablePartDefinition".

The Problem is, that I don't know why it is added again to the "_activatedParts" Collection. Maybe someone can explain that to me and tell me how it is possible to remove "ComposablePartDefinitions" one by one.

Thanks in advance for all answers.