This project has moved and is read-only. For the latest updates, please go here.

CompositionContainer container.GetExportedValues<IModuleController>();

May 14, 2012 at 9:27 AM

I am trying to "rebuild" the appliation ... to understand how the MEF is working.

I do have the problem that



>(); ... cannot list any of the IModuleController exports.


 Am i missing just a simple thing?



May 14, 2012 at 10:55 AM


Put break point in the contractor of class that implement IModuleController interface and trace to see what is the problem. I think some of dlls not exist in output folder.

Hope Helps,


May 14, 2012 at 11:28 AM

Thank you for the immediate reply.

I defined the dll output (debug) folder as the code example shows, all the necessary files (dlls) are in there.

If i wanne step through the code .... breakpoint at


>(); ... i cannot step through each class ... that implements IModuleController ... its actually almost the same code as in your example.

The container "holds" all the necessary classes/interfaces ...


May 14, 2012 at 1:15 PM

Some code might be helpful, although it is almost the same as the WAF example:

Here i am trying to load the exported values:

using System;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Waf;
using System.Waf.Applications;
using System.Windows;
using System.Windows.Threading;
using ApplicationStory.Applications.Controllers;
using System.Collections.Generic;
using ApplicationStory.Applications.ViewModels;
using ApplicationStory.Presentation.Properties;

namespace ApplicationStory.Presentation
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
        private static readonly bool isInDesignMode = DesignerProperties.GetIsInDesignMode(new DependencyObject());
        private CompositionContainer container;
        private IEnumerable<IModuleController> moduleControllers;

        static App()
#if (DEBUG)
            WafConfiguration.Debug = true;

        public static bool IsInDesignMode { get { return isInDesignMode; } }

        protected override void OnStartup(StartupEventArgs e)

#if (DEBUG != true)
            // Don't handle the exceptions in Debug mode because otherwise the Debugger wouldn't
            // jump into the code when an exception occurs.
            DispatcherUnhandledException += AppDispatcherUnhandledException;
            AppDomain.CurrentDomain.UnhandledException += AppDomainUnhandledException;

            AggregateCatalog catalog = new AggregateCatalog();
            // Hinzufügen der WpfApplicationFramework-Assembly zum Katalog
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(Controller).Assembly));
            // Hinzufügen der ApplicationStory.Presentation-Assembly zum Katalog
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            // Hinzufügen der ApplicationStory.Applications-Assembly zum Katalog
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(ShellViewModel).Assembly));

            // Laden der Module-Assemblies (siehe App.config)
            if (Settings.Default.ModuleAssemblies != null)
                foreach(string moduleAssembly in Settings.Default.ModuleAssemblies)
                    catalog.Catalogs.Add(new AssemblyCatalog(moduleAssembly));

            container = new CompositionContainer(catalog);
            CompositionBatch batch = new CompositionBatch();

            moduleControllers = container.GetExportedValues<IModuleController>();

            bool any = false;
            foreach (IModuleController controller in moduleControllers)
                any = true;
                throw new Exception(string.Format("Could not locate any instances!"));

            foreach (IModuleController moduleController in moduleControllers) { moduleController.Initialize(); }
            foreach (IModuleController moduleController in moduleControllers) { moduleController.Run(); }


And this is a class that implements the IModuleController:

    internal class ApplicationStoryController : Controller
        private readonly CompositionContainer container;
        private readonly IShellService shellService;
        private readonly IEntityService entityService;
        private readonly ApplicationStoryViewModel applicationStoryViewModel;
        private readonly DelegateCommand addNewCommand;
        private readonly DelegateCommand removeCommand;
        private readonly DelegateCommand anforderungToCommand;
        private ApplicationStoryListViewModel storyListViewModel;
        private SynchronizingCollection storyDataModels;

        public ApplicationStoryController(CompositionContainer container, IShellService shellService, IEntityService entityService,
            ApplicationStoryViewModel storyViewModel)
            this.container = container;
            this.shellService = shellService;
            this.entityService = entityService;
            this.applicationStoryViewModel = storyViewModel;
            this.addNewCommand = new DelegateCommand(AddNewStory, CanAddNewStory);
            this.removeCommand = new DelegateCommand(RemoveStory, CanRemoveStory);
            this.anforderungToCommand = new DelegateCommand(p => AnforderungTo((ApplicationStory.Domain.ApplicationStory)p));

        public void Initialize()
            applicationStoryViewModel.AnforderungToCommand = anforderungToCommand;
            AddWeakEventListener(applicationStoryViewModel, storyViewModelPropertyChanged);

            IApplicationStoryListView storyListView = container.GetExportedValue();
            storyDataModels = new SynchronizingCollection(entityService.Stories,
                b => new ApplicationStoryDataModel(b, anforderungToCommand));
            storyListViewModel = new ApplicationStoryListViewModel(storyListView, storyDataModels);
            storyListViewModel.AddNewCommand = addNewCommand;
            storyListViewModel.RemoveCommand = removeCommand;
            AddWeakEventListener(storyListViewModel, StoryListViewModelPropertyChanged);

            shellService.ApplicationStoryListView = storyListViewModel.View;
            shellService.ApplicationStoryView = applicationStoryViewModel.View;

            storyListViewModel.SelectedStory = storyListViewModel.Stories.FirstOrDefault();
        private bool CanAddNewStory() { return applicationStoryViewModel.IsValid; }

        private void AddNewStory()
            ApplicationStory.Domain.ApplicationStory story = new ApplicationStory.Domain.ApplicationStory();

            storyListViewModel.SelectedStory = storyDataModels.Single(b => b.Story == story);

        private bool CanRemoveStory() { return storyListViewModel.SelectedStory != null; }

        private void RemoveStory()
            // Use the StoryCollectionView, which represents the sorted/filtered state of the stories, to determine the next story to select.
            IEnumerable booksToExclude = storyListViewModel.SelectedStories.Except(new[] { storyListViewModel.SelectedStory });
            ApplicationStoryDataModel nextStory = CollectionHelper.GetNextElementOrDefault(storyListViewModel.StoryCollectionView.Except(booksToExclude),

            foreach (ApplicationStoryDataModel story in storyListViewModel.SelectedStories.ToArray())

            storyListViewModel.SelectedStory = nextStory ?? storyListViewModel.StoryCollectionView.LastOrDefault();

        private void UpdateCommands()

        private void StoryListViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
            if (e.PropertyName == "SelectedStory")
                applicationStoryViewModel.Story = storyListViewModel.SelectedStory != null ? storyListViewModel.SelectedStory.Story : null;

        private void storyViewModelPropertyChanged(object sender, PropertyChangedEventArgs e)
            if (e.PropertyName == "IsValid")

The module controller itself:

[Export(typeof(ApplicationStory.Applications.Controllers.IModuleController)), Export]
    internal class ModuleController : Controller, IModuleController
        private readonly IMessageService messageService;
        private readonly IEntityController entityController;
        private readonly ApplicationStoryController storyController;
        private readonly ApplicationStoryAnforderungController anforderungController;
        private readonly IShellService shellService;
        private readonly ShellViewModel shellViewModel;
        private readonly DelegateCommand exitCommand;

        public ModuleController(IMessageService messageService, IPresentationService presentationService,
            IEntityController entityController, ApplicationStoryController storyController, ApplicationStoryAnforderungController anforderungController,
            ShellService shellService, ShellViewModel shellViewModel)

            this.messageService = messageService;
            this.entityController = entityController;
            this.storyController = storyController;
            this.anforderungController = anforderungController;
            this.shellService = shellService;
            this.shellViewModel = shellViewModel;

            shellService.ShellView = shellViewModel.View;

            this.shellViewModel.Closing += ShellViewModelClosing;
            this.exitCommand = new DelegateCommand(Close);

        public void Initialize()
            shellViewModel.ExitCommand = exitCommand;


        public void Run()

        public void Shutdown()

            catch (Exception)
                // When more application instances are closed at the same time then an exception occurs.

        private void ShellViewModelClosing(object sender, CancelEventArgs e)
            if (entityController.HasChanges)
                if (entityController.CanSave())
                    bool? result = messageService.ShowQuestion(shellService.ShellView, Resources.SaveChangesQuestion);
                    if (result == true)
                        if (!entityController.Save())
                            e.Cancel = true;
                    else if (result == null)
                        e.Cancel = true;
                    e.Cancel = !messageService.ShowYesNoQuestion(shellService.ShellView, Resources.LoseChangesQuestion);

        private void Close()


so it is simply ananogous to the example given here. And i still cannot figure out, why it is not working.

Thanks for you time.


Jan 29, 2013 at 4:18 PM

Hi, I have the same issue, did any body solved the problem for you?