Repro-Solution for problems with MEF recomposition in injected ViewModels


The attached VS2010 solution illustrates a problem with using a DeploymentCatalogService and MEF recomposition in ViewModels injected into their corresponding View via MEFedMVVM's ViewModelLocator mechanism.

file attachments


DrJ wrote Oct 1, 2010 at 11:50 AM

Further analysis leads me to believe that my problem of not getting [Imports] recomposed in a ViewModel attached via the MEFedMVVM ViewModelLocator on loading new ComposableParts via a DeploymentCatalogService may be due to a bug in the MEF implementation of CatalogExportProvider.
To me it looks as though only the Changing-Event of an INotifyComposablePartCatalogChanged passed into the CatalogExportProvider() constructor is being connected to the CatalogExportProvider while the corresponding Changed-Event is left disconnected.
Could this lead to the attached [Imports] of an injected ViewModel not being notified of recompositions?

From System.ComponentModel.Composition.Hosting.CompositionContainer via Reflector:
  public CatalogExportProvider(ComposablePartCatalog catalog, bool isThreadSafe)
     this._activatedParts = new Dictionary<ComposablePartDefinition, ComposablePart>();
     this._rejectedParts = new HashSet<ComposablePartDefinition>();
     this._conditionalReferencesForRecomposableParts = new ConditionalWeakTable<object, List<ComposablePart>>();
     this._partsToDispose = new HashSet<IDisposable>();
     this._isDisposed = false;
     this._isRunning = false;
     Requires.NotNull<ComposablePartCatalog>(catalog, "catalog");
     this._catalog = catalog;
     INotifyComposablePartCatalogChanged changed = this._catalog as INotifyComposablePartCatalogChanged;
     if (changed != null) {
        changed.Changing += new EventHandler<ComposablePartCatalogChangeEventArgs>(this.OnCatalogChanging);

     /* This is where I suspect the following line is missing:
     changed.Changed += new EventHandler<ComposablePartCatalogChangeEventArgs>(this.OnCatalogChanged);

     this._lock = new CompositionLock(isThreadSafe);

marlongrech wrote Oct 2, 2010 at 11:46 AM

I see the issue... and I must say I have no idea about that... I tried making both ViewModels (the mef on and the MefedMVVM one) implement IPartImportsSatisfiedNotification and I see the MEF view model being called upon recomposition but the MEFedMVVM one nada... this is quite strange... I will dig a bit more...

Thanks for reporting it

marlongrech wrote Oct 2, 2010 at 11:54 AM

mmm... I think this is a limitation of MEFedMVVM... in order to resolve the object I do Container.GetExports(definition) while the CompositionInitializer does globalContainer.Compose(batch);

having said this, recomposition is a bit of a dangerous concept... I am in the process of doing an article on how to do multiple xaps for an app... Should be interesting for you... (and I do not use Recomposition). Do not get me wrong I am not saying I will not look into this, I am just suggesting an alternative.

P.S Sorry for taking so long to respond but right now I am close to a release at work and doing CRAZY hours ...

DrJ wrote Oct 5, 2010 at 4:39 PM

Sad to read that this may be a limitation of MEFedMVVM - otherwise I've come to like it for its ease of use and streamlined appearance. Now if you only came around to making NotifyPropertyChangedBase.OnPropertyChanged(...) virtual ... :-]

Back to the issue at hand: I have a requirement for my application to support multiple plugins - not for startup time optimization but for customization purposes - and MEF+DeploymentCatalogService+MEFedMVVM seemed like the straightforward way to go. Which led me down the path to MEF recomposition due to DeploymentCatalog's (and Silverlight's in general) asynchrous I/O behavior, although - unlike the Repro-.sln - my actual project initiates plugin download right at the start of the application.

So if you can suggest a different way of loading a runtime-configurable set of plugin-XAPs and having their Exports included in MEF composition while still reaping all the benefits of MEFedMVVM, then I'm all ears.

wrote Nov 17, 2011 at 11:43 AM

danneesset wrote Nov 17, 2011 at 11:46 AM

I also had problems with recomposition and it turns out that the CatalogExportProvider doesnt support change notifications.
Have a look at patch 10075 which I supplied some time ago and see if it solves your problem (link to patches http://mefedmvvm.codeplex.com/SourceControl/list/patches),

marlongrech wrote Nov 22, 2011 at 7:03 AM

Hey thanks for the patch I applied it in the latest MEFed... Thanks for all the help !

wrote Feb 14, 2013 at 6:47 PM