1

Below is my scenario. I am trying to associate the types to the interfaces by using the Export functionality during the initial bootstrap. However, MEF complains on ImportCardinalityMismatchException.I am fairly new to MEF and I couldn't figure out what's wrong here? The easiest fix is to remove the inheritance. However, I would like to avoid it.

   public interface IColourService
    {
        Color GetColourByCountry(string countryName);
    }

    public interface IKnownColourService:IColourService
    {
        bool IsKnownCountry(string countryName);
    }

    public interface IUnKnownColourService:IColourService
    {
       bool IsUnKnownCountry(string countryName);
    }

    [Export(typeof(IColourService))]
    public class ColourService:IColourService
    {
       //implementation
    }

    [Export(typeof(IKnownColourService))]   
    public class KnownColourService:IKnownColourService
    {
       //implementation
    }

    [Export(typeof(IUnKnownColourService))]
    public class UnknownColourService:IUnKnownColourService
    {
       //implementation
    }
Hunter
  • 2,370
  • 2
  • 20
  • 24
  • 1
    Your problem may be that you have three valid `IColorService` objects, when MEF expects to only find one. I'd expect that to be fixed by using named imports. If I felt strongly enough that this was the problem, this would be an answer. – Magus Apr 02 '14 at 15:57
  • @Magus will check named imports. – Hunter Apr 02 '14 at 15:58

1 Answers1

0

Can you not use IColourService as the Type in the Export attribute for all the classes?

[Export(typeof(IColourService))]

Then, you can access them with the following property declaration:

[ImportMany]
public IEnumerable<IColourService> ColourServices { get; set; }

And add a helper method to get specific types:

public IEnumerable<T> GetServices<T>()
{
    return ColourServices.OfType<T>().ToList();
}

Hope this helps...

John Askew
  • 233
  • 1
  • 12