CompositionFailedException while CompostionHost.SatisfyImports(...)

Jun 4, 2012 at 1:07 PM
Edited Jun 4, 2012 at 1:09 PM

Hi,

I'm working with the MEF Version include in .Net 4.5 RC (Win8 RP). CompostionService is gone for Metro style Apps, so I had to rewrite my code and got the following:

 

    internal class ExtensionManager
    {
        [ImportMany]
        public ObservableCollection<IExtension> Imports { get; set; }

        private ExtensionManager()
        {
            Imports = new ObservableCollection<IExtension>();
            LoadExtensions();
        }

        private void LoadExtensions()
        {
            var testing = new ContainerConfiguration().WithAssemblies(
                    new ArraySegment<Assembly>(new[] {
                                                       typeof (App).GetTypeInfo().Assembly,
                                                       Assembly.Load(new AssemblyName("Extensions.Extension1")),
                                                       Assembly.Load(new AssemblyName("Extensions.Extension2"))
                                                   }));

            using(var cont = testing.CreateContainer())
            {
                cont.GetExports(typeof (IExtension));
                cont.SatisfyImports(this);
            }
        }
    }
}

 

while executing cont.SatisfyImports(this); I get the following exception:

 

System.Composition.Hosting.CompositionFailedException occurred
  HResult=-2146233088
  Message=Missing dependency 'Imports' on 'Classes.ExtensionManager'.
  Source=System.Composition.TypedParts
  StackTrace:
       at System.Composition.CompositionContextExtensions.SatisfyImportsInternal(CompositionContext exportProvider, Object objectWithLooseImports, AttributedModelProvider conventions)
       at System.Composition.CompositionContextExtensions.SatisfyImports(CompositionContext compositionContext, Object objectWithLooseImports)
       at Classes.ExtensionManager.LoadExtensions() in path\Classes\ExtensionManager.cs:line 109
  InnerException: 

 

I don't see the problem nor do I understand the error message. Working with Import instead of ImportMany works fine. Any help would be nice!

Thanks and kind regards

Jan

Jun 5, 2012 at 4:32 PM

Hi - thanks for getting in touch. I think we missed this in the release notes.

Because recomposition is no longer supported, [ImportMany] collection support has been simplified and only works with:

  • T[]
  • IEnumerable<T>
  • ICollection<T>
  • IList<T>

If your scenario can use one of these types for the property, that's the simplest approach; you don't need to initialize the property to an instance, the container will set it to a new object.

There are a few alternatives that enable observable collections, the best one for you depends on how you use the ExtensionManager class. The most straightforward solution is:

var container = testing.CreateContainer();

foreach (var extension in container.GetExports<IExtension>())

    Imports.Add(extension);

Also, the container needs to 'live' as long as you want to use the parts in it, so I would avoid the "using" block in this code.

Hope this helps, let me know if you'd like any clarification.

Nick

 

Jun 6, 2012 at 7:46 AM

Hello Nick,

thanks for your answer! I simply switched to ICollection<T> and all works fine! I already build the second solution you provided as a workaround. But loading each extension, was not really a nice solution.

 

Thank you!

Jan