Search code examples
c#c#-4.0mef

MEF, ImportingConstructor seems to be causing an error


I keep getting the following error with the following code:

Error: "No exports were found that match the constraint: 

ContractName    MefTestSample.Contracts.ICanDoSomethingImportant"

Program.cs is as follows:

namespace MefTestSample
{
    class Program
    {
        private static CompositionContainer m_Container;


        static void Main(string[] args)
        {
            UseMockedUpTypes();
            ICanDoSomethingImportant cool = m_Container.GetExport<ICanDoSomethingImportant>().Value;
            cool.DoSomethingClever();
            Console.ReadLine();
        }

        private static void UseMockedUpTypes()
        {
            //The commented out section works just by itself.  
            /*
            m_Container =
                new CompositionContainer(
                    new AssemblyCatalog(
                        typeof(MefTestSample.Mocks.ClassesDoNotNecessarly).Assembly));
             */

            //This fails if i dont comment out the [ImportingConstructor] block.
            var assemblyCatalog1 = new AssemblyCatalog(typeof (MefTestSample.Mocks.ClassesDoNotNecessarly).Assembly);
            var myassembly = new AssemblyCatalog(typeof (ServiceLibrary.MoonService).Assembly);
            var aggregateCatalog = new AggregateCatalog(assemblyCatalog1, myassembly);
            m_Container = new CompositionContainer(aggregateCatalog);
        }
    }
}

Below is the code for ClassesDoNotNecessarly:

namespace MefTestSample.Mocks
{
    [Export(typeof(ICanDoSomethingImportant))]
    public class ClassesDoNotNecessarly : ICanDoSomethingImportant
    {
        //private IServicesContract _isc;
        #region ICanDoSomethingImportant Members

        /* This seems to be causing the problem.
        [ImportingConstructor]
        public ClassesDoNotNecessarly([Import("Moon")]IServicesContract isc)
        {
            string temp = isc.DisplayMessage();
            Console.WriteLine(temp);
        }
        */

        public void DoSomethingClever()
        {
            Console.WriteLine("Hehe, I'm actually procrastinating!");        
        }

        #endregion
    }
}

MoonService is as follows:

namespace ServiceLibrary
{
    [Export(typeof(IServicesContract))]
    public class MoonService : IServicesContract
    {
        public string DisplayMessage()
        {
            return "Moon services were accessed.";
        }
    }
}

What i believe the problem is. If i leave program.cs as it is, and comment out the [ImportingConstructor] attribute + constructor in the ClassesDoNotNecessarly class, the program will display the text in that class.

If i uncomment the [ImportingConstructor] attribute n constructor, i then start getting the error shown at the top of this question.

Any ideas would be appreciated.


Solution

  • Remove the [Import("Moon")] in the constructor, the Export is not named, so the Import cannot be named in return.