tags:

views:

205

answers:

2

I have been trying to use the configurable provider model for handling my MEF imports and exports from MEF Contrib (link). I've read the Codeplex documentation and Code Junkie's blog post (link); however, I can't seem to get the container to create the parts. Where am I going wrong?

Program.cs

namespace MEFTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
        }

        // [ImportMany("command", typeof(IHelp))]
        public IEnumerable<IHelp> Commands { get; set; }

        void Run()
        {
            Compose();

            foreach(IHelp cmd in Commands)
            {
                Console.WriteLine(cmd.HelpText);
            }

            Console.ReadKey();
        }

        void Compose()
        {
            var provider = new ConfigurableDefinitionProvider("mef.configuration");
            var catalog = new DefinitionProviderPartCatalog<ConfigurableDefinitionProvider>(provider);
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
 }
}

TestCommand.cs

namespace MEFTest
{
    //[Export("command", typeof(IHelp))]
    public class TestCommand : IHelp
    {
        private string _helpText = "This is a test.";

        public string CommandName
        {
            get { return "Test"; }
        }

        public string HelpText
        {
            get { return _helpText; }
        }
    }
}

App.Config section:

<mef.configuration>
 <parts>
  <part type="MEFTest.TestCommand, MEFTest">
   <exports>
    <export contract="IHelp" />
   </exports>
  </part>
  <part type="MEFTest.Program, MEFTest">
   <imports>
    <import member="Commands" contract="IHelp" />
   </imports>
  </part>
 </parts>
</mef.configuration>

I don't get any build errors and it runs fine if I switch to the typical attribute-based system that is part of the MEF core (with the appropriate catalog too). Program.Commands is always NULL in the above example. I tried to just use a singular property instead of a collection and get the same results.

When I debug I can get the provider.Parts collection so I know it's accessing the configuration information correctly; however, I get an InvalidOperationException whenever I debug and try to drill into catalog.Parts.

Anyone have any experience as to where I'm going wrong here?

A: 

As documented here, you also need this in your config file:

<configSections>
  <section
    name="mef.configuration"
    type="MefContrib.Models.Provider.Definitions.Configurable.PartCatalogConfigurationSection, MefContrib.Models.Provider" />
</configSections>

If you already have that, then it might be interesting to show us the stack trace of the InvalidOperationException that you get when accessing provider.Parts.

Wim Coenen
A: 

I had the same problems and could not get it to work, but here are some details: It seems that ComposeParts() does not work as expected (at least in the version I used) because it uses static methods, based on Reflection to find all required Imports (so it seems that this part cannot be changed from outside of MEF). Unfortunately we want to use xml configuration and not the MEF attributes.

It works if you add [Import] attributes to the members of the class you you use with ComposeParts(). In your case this would be "Programm". In this case all exports defined in the configuration file will be found.

I could not find any documentation or examples on the MEF Contrib page relating to that problem. Also there is no unittest in the MEF contrib projekt that uses ComposeParts(). A workaround would be to use container.GetExportedValues() to retrieve the values, but in this case you have to set the classes members manually.

Hope that helps.

Bernhard Kircher

related questions