Export issue

Oct 28, 2009 at 4:30 PM

Hello,

I try MEF (latest version Preview 8) and I have some difficulties.
I raise an export error : ...No valid exports were found that match the constraint ‘((exportDefinition.ContractNane ...
I think it is very simple but ....

If neccessary my source code is available
Thank you very much
Sebastien

-------------------------------------------------------------------------------------
See following my architecture
the Contract:

namespace Contracts
{
    public interface IOperation
    {
        decimal Calcule();
    }
}

The composant
namespace MEFDemo
{
    [Export(typeof(IOperation))]
    public class Addition : IOperation
    {
        [Export]
        public decimal Calcule()
        {
            MessageBox.Show("Addition", "MM Addition");
            return 54;
        }
    }
}

The Client
namespace Client
{
    public partial class frmCalcule : Form
    {
        [Import]
        IEnumerable<IOperation> operations;

        public frmCalcule()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                var catalog = new DirectoryCatalog(@"..\..\..\MMCalcule\bin\debug");
                var container = new CompositionContainer(catalog);

                var batch = new CompositionBatch();
                batch.AddPart(this);
               
                container.Compose(batch);

                foreach (var operation in operations)
                {
                    decimal z = operation.Calcule();
                    this.label1.Text = "The result :" + z;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

Developer
Oct 28, 2009 at 4:46 PM

[Import]
IEnumerable<IOperation> operations;

It looks like you are trying to import all the operations and in which case you need [ImportMany] instead of [Import].

Also I don't believe you actually want to put the [Export] on the Calculate method.

Oct 28, 2009 at 4:51 PM

You are my new best friend !

It is working very well, thanks a lot.

Sébastien

Nov 4, 2009 at 4:37 PM
you should try ComposeParts instead of Compose

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

namespace ConsoleApplication4MEF
{
    class Program
    {
        static void Main(string[] args)
        {
            var foo = new Foo();
            var container = new CompositionContainer();
            container.ComposeParts(foo);
            var f = container.GetExportedValue<IFoo>();
        }
    }

    [Export( typeof(IFoo))]
    public class Foo : IFoo
    { 
    }

    public interface IFoo
    { 
    }
}