Prism+MEF+IServiceLocator: can't find a component registered with GetExportedValue in ServiceLocator.Current

Topics: Prism v4 - WPF 4
Aug 9, 2010 at 4:46 PM
Edited Aug 9, 2010 at 4:49 PM
Hi! I'm trying to kick off with Prism4. (disclaimer: I have no experience with previous versions of Prism) I've choosen to use MEF as container. So I'm using MefBootstapper as the base for my bootstrapper. There's ConfigureContainer method in MefBootstrapper with such code: [code] this.Container.ComposeExportedValue<ILoggerFacade>(this.Logger); this.Container.ComposeExportedValue<IModuleCatalog>(this.ModuleCatalog); this.Container.ComposeExportedValue<IServiceLocator>(new MefServiceLocatorAdapter(this.Container)); [/code] Here we (you) register a wrapper for well-known IServiceLocator implementation. Then in another method, ConfigureServiceLocator, we set up a IServiceLocator-implementation from the Container as "global" Service Locator : [code] protected override void ConfigureServiceLocator() { IServiceLocator serviceLocator = this.Container.GetExportedValue<IServiceLocator>(); ServiceLocator.SetLocatorProvider(() => serviceLocator); } [/code] MefBootstapper.Run's logic sequence is : ConfigureContainer -> ConfigureServiceLocator Then in some module I call: var f = ServiceLocator.Current.GetInstance<ILoggerFacade>(); and got this exception: ActivationException: Activation error occured while trying to get instance of type ILoggerFacade, key ""/ Actually I ask not for ILoggerFacade, it's just for an example. The key point here is that I ask for an interface which implementation I registered in ConfigureContainer as "Container.ComposeExportedValue". I understand that I can get all required references in my module's constructor (via ImportingConstructor), but : 1) ServiceLocator-approach should work too, doesn't? 2) I have a base class for all my modules and don't want to burden all their constructors with additional parameters (which should be just transfered to base ctor) I'm using Prism v4 CTP (August).
Aug 9, 2010 at 4:52 PM
Sorry for the formatting, but I can't understand why is that, editor just ignores all whitespaces.
Aug 9, 2010 at 6:55 PM
solved. ServiceLocator works as expected. The issue was cause by mismatch of contracts specified for ComposeExportedValue and ServiceLocator.GetInstance: class Service: IService {} was this.Container.ComposeExportedValue(service) instead of this.Container.ComposeExportedValue<IService>(service) So here ServiceLocator could't find "IService" as only Service was registred: var service = Current.GetInstance<IService>();