ExportFactory - not able to find export

Aug 12, 2010 at 8:00 AM
Edited Aug 13, 2010 at 4:14 AM

I am using ExportFactory to instantiate export dynamically but when I run my application ... it gives me error while composing parts in program.cs class shown below:

The composition remains unchanged. The changes were rejected because of the following error(s): The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information.

1) No valid exports were found that match the constraint '((exportDefinition.ContractName == "ExportFactoryApp.Factories") AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") AndAlso "ExportFactoryApp.Factories".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', invalid exports may have been rejected.

Resulting in: Cannot set import 'ExportFactoryApp.Program.factories (ContractName="ExportFactoryApp.Factories")' on part 'ExportFactoryApp.Program'.
Element: ExportFactoryApp.Program.factories (ContractName="ExportFactoryApp.Factories") -->  ExportFactoryApp.Program.

 

Here is my code :

1. Program.cs class.

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace ExportFactoryApp
{
    public class Program 
    {
        [Import]
        public Factories factories { get; set; }


        static void Main(string[] args)
        {
            Program p = new Program();
            p.Run();
        }
               
        private void Run()
        {
            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);

            var iFactory = factories.GetContract();
            Console.ReadKey();
        }
    }
}

 

2. Factories class

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;

namespace ExportFactoryApp
{
    [Export]
    public class Factories
    {
        [Import]
        public ExportFactory<IFactory> factory { get; set; }
        
        public Factories()
        {
            CompositionInitializer.SatisfyImports(this);
        }

        public IFactory GetContract()
        {
            return factory.CreateExport().Value;
        }


    }
}

 

3. Finally Factory.cs class & IFactory.cs interface

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel;

namespace ExportFactoryApp
{
    [Export(typeof(IFactory))]
    public class FactoryClass: IFactory
    {

        public string Message
        {
            get
            {
                return "test";
            }
        }
    }
    public interface IFactory
    {
        string Message { get;  }
    }
}

 

Please let me know If I have done something wrong which is affected on ExportFactory property....

- Thanks