1

I'm coming across some strange MEF behaviour in Prism, which I can't really explain. I've found a way around it that I'm not too happy with, so I'd really like to understand what's causing it.

I've declared my shell window class with a PartCreationPolicy of NonShared. And I'm trying to use the CompositionContainer.GetExportedValue<>() function from my MefBootstrapper to create a new instance of the Shell.

The strange thing is, if I call Container.GetExportedValue<>() before the shell has been created, I get a new object of type Shell, each time I call it. However, once the shell has been initialized, repeated calls to Container.GetExportedValue<>() return the same instance of the Shell.

It's as if the shell initialization somehow re-registers my Shell export as a Shared.

However, I don't see any calls in the bootstrapper code that explicitly try to achieve this.

Can anyone explain:

  1. what action has this side effect
  2. How (if possible) to restore the NonShared behaviour, so I can create multiple shells using MEF/ServiceLocator.

Cheers,

Mark

Mark
  • 1,784
  • 16
  • 26
  • just some thoughts. if GetExportedValue works with Lazy under the hood, then your CreationPolicy NonShared will not work, because Lazy.Value is always shared. – blindmeis Oct 20 '11 at 10:59
  • This doesn't explain why I get different instances before initializing the shell. – Mark Oct 20 '11 at 12:52

2 Answers2

0

here is a answer for your multiple shell question. you have to check if the NonShared behavior is answered there.

Community
  • 1
  • 1
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • I have multiple shells working already. My question relates to the instantiation of the shell object. – Mark Oct 20 '11 at 12:53
0

I'm not sure how Prism uses MEF, but here's a theory: How is the shell being created in normal startup? My guess is that it is not by calling GetExportedValue from the MEF container, but rather by calling the constructor for the Shell and then adding it to the container via ComposeParts() or using a CompositionBatch. A part added directly to the container in that way would override what was available in the catalog, and the CreationPolicy wouldn't apply either (because MEF isn't creating that part).

Daniel Plaisted
  • 16,674
  • 4
  • 44
  • 56
  • Yes, I think the MEF Bootstrapper in Prism calls `ComposeParts`. I didn't realise the implications of this. – Mark Oct 24 '11 at 15:27