CompositionContainer container.GetExportedValues<IModuleController>();

May 14, 2012 at 8:27 AM

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

I do have the problem that

container.GetExportedValues<

IModuleController

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

 

 Am i missing just a simple thing?

 

Thanks.

May 14, 2012 at 9:55 AM

Hi,

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,

Asad

May 14, 2012 at 10: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

container.GetExportedValues<IModuleController

>(); ... 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 ...

Katja

May 14, 2012 at 12: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;
#endif
        }


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


        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(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;
#endif

            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();
            batch.AddExportedValue(container);
            container.Compose(batch);

            moduleControllers = container.GetExportedValues<IModuleController>();

            bool any = false;
            foreach (IModuleController controller in moduleControllers)
            {
                any = true;
                break;
            }
            if(!any)
                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:

[Export]
    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;


        [ImportingConstructor]
        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();
            entityService.Stories.Add(story);

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

        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),
                storyListViewModel.SelectedStory);

            foreach (ApplicationStoryDataModel story in storyListViewModel.SelectedStories.ToArray())
            {
                entityService.Stories.Remove(story.Story);
            }

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

       
        private void UpdateCommands()
        {
            addNewCommand.RaiseCanExecuteChanged();
            removeCommand.RaiseCanExecuteChanged();
            anforderungToCommand.RaiseCanExecuteChanged();
        }

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

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

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;


        [ImportingConstructor]
        public ModuleController(IMessageService messageService, IPresentationService presentationService,
            IEntityController entityController, ApplicationStoryController storyController, ApplicationStoryAnforderungController anforderungController,
            ShellService shellService, ShellViewModel shellViewModel)
        {
            presentationService.InitializeCultures();

            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;

            entityController.Initialize();
            storyController.Initialize();
            anforderungController.Initialize();
        }

        public void Run()
        {
            shellViewModel.Show();
        }

        public void Shutdown()
        {
            entityController.Shutdown();

            try
            {
                Settings.Default.Save();
            }
            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;
                    }
                }
                else
                {
                    e.Cancel = !messageService.ShowYesNoQuestion(shellService.ShellView, Resources.LoseChangesQuestion);
                }
            }
        }

        private void Close()
        {
            shellViewModel.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.

Katja

Jan 29, 2013 at 3:18 PM

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

Regards

Moe