Using MEF to compose parts of an XBAP application

Jul 27, 2010 at 7:26 PM
I think this has been asked before, but I am trying to use MEF to compose parts of an XBAP application. I have added the parts to the project, so they get included in the manifest, and they are downloaded. When I compose the parts, the assemblies appear to be loading, but fail to get composed. Can anyone suggest where I am going wrong - I can post the code if required. Thanks
Jul 27, 2010 at 10:37 PM

Code would be helpful. Thanks

Jul 28, 2010 at 8:28 AM
Edited Jul 28, 2010 at 11:21 AM
Hi, The code I am using is as below ...
// Compose from the App.xaml.cs

        private bool Compose()
        {
            var catalog = new AggregateCatalog();
            //Adds all the parts found in all assemblies in 
            //the same directory as the executing program

//            MessageBox.Show("Looking in " + Path.GetDirectoryName(
//               Assembly.GetExecutingAssembly().Location));

// *** NOTE: At this point the path looks correct

            catalog.Catalogs.Add(
             new DirectoryCatalog(
              Path.GetDirectoryName(
               Assembly.GetExecutingAssembly().Location)));
            catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new AssemblyCatalog(typeof(IPageInterface).Assembly));

            //MessageBox.Show(catalog.Catalogs.Count.ToString()); 

// *** NOTE: The count of catalogs is 3 (which is correct).

            container = new CompositionContainer(catalog);
            CompositionBatch batch = new CompositionBatch();
            batch.AddPart(this);

            try
            {
// **** At this point batch contains PartsToAdd = 1, PartsToRemove = 0
// **** container.Catalog.Parts contains 5 entries, which appear to be the assemblies I want
                container.Compose(batch);
            }
            catch (CompositionException compositionException)
            {
                MessageBox.Show(compositionException.ToString());
                Shutdown(1);
                return false;
            }

            return true;
        }

// From the Page1.xaml.cs

    public partial class Page1 : Page
    {
        ...

        [ImportMany(typeof(IPageInterface), AllowRecomposition = true)]
        private IEnumerable<Lazy<IPageInterface, IShellViewMetadata>> pages { get; set; }

        [Import(typeof(IConversation), AllowRecomposition = true)]
        private IConversation Conv { get; set; }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
/// NOTE: Conv is null
            if (Conv == null)
            {
                MessageBox.Show("No conversation loaded", "DemoWpfBrowserApplication");                    
            }

/// NOTE: pages is null

            if (pages != null)
            {
                foreach (var page in pages.OrderBy(i => i.Metadata.Index))
                {
                    TabItem item = new TabItem();
                    item.Header = page.Value.PageTitle;

                    Frame frame = new Frame();

                    frame.Content = page.Value.View;

                    item.Content = frame;

                    // Add each page
                    tcPageControl.Items.Add(item);
                }
            }
            else
            {
                MessageBox.Show("No pages loaded", "DemoWpfBrowserApplication");
            }
        }
        ...
}
So it seems to load the assemblies, but not put anything into pages, and hence "No pages loaded". The same basic code works in a standard MEF application, I am clearly doing something wrong somewhere, but am a bit lost now. Thanks in advance
Jul 28, 2010 at 4:58 PM

I can't get ANY of the MEF stuff to work and ALL the examples I've found on the Internet are outdated and won't compile with the latest releases of VS2010 and SL4. I wonder if anyone has successfully implemented a solution with the latest releases that actually loads parts on demand.

Jul 28, 2010 at 5:12 PM

Have you tried this one? 

http://blogs.msdn.com/b/gblock/archive/2010/07/28/9974664.aspx

Jul 28, 2010 at 7:33 PM
I have MEF working in a 'normal' WPF application, so this is based on a working example, it just doesn't seem to work in conjunction with XBAP
Jul 29, 2010 at 2:33 PM

I think we've had the same problem look at http://mef.codeplex.com/Thread/View.aspx?ThreadId=221818