MEF throws exception ImportCardinalityMismatchException?
Asked Answered
E

1

7

What is a possible reason for MEF throwing an ImportCardinalityMismatchException?

Equivalence answered 19/9, 2013 at 9:14 Comment(0)
E
5

The code below demonstrates how to reproduce this error.

To test, make sure you compile with .NET 4.5, and add the MEF assemblies:

  • System.ComponentModel.Composition
  • System.ComponentModel.Composition.Registration

The problem is that MEF wants to construct a Person object, but it cant finish its property injection for "Age" (which is marked as "Import").

To reproduce the error, comment out the line marked below.

using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace ForStackOverflow
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var container = new CompositionContainer(
                new AssemblyCatalog(Assembly.GetExecutingAssembly()));

            // Comment out this next line to get an
            // ImportCardinalityMismatchException error
            container.ComposeExportedValue("Age", 30); 

            var person = container.GetExportedValue<Person>();

            Console.WriteLine("Persons age: " + person.Age);
            Console.WriteLine("[press any key to continue]");
            Console.ReadKey();
        }
    }

    [Export]
    public class Person
    {
        [ImportingConstructor]
        public Person()
        {
        }

        [Import("Age")]
        public int Age { get; set; }
    }
}
Equivalence answered 19/9, 2013 at 9:14 Comment(7)
Its an answer to my own question. I sometimes use StackOverflow as a store for issues I have encountered in the past.Equivalence
This is great but... How do I find WHICH import is breaking it?Senator
That's the million dollar question. MEF is not really designed as a complete dependency injection solution, and is mising many of the features that other DI containers have. In particular, it's difficult to find the cause of a MEF error as their error messages are not very informative. You really need to be committing to source control regularly, so you can roll back if you get an error. The main point to note is that this error is caused by the DI container attempting to resolve a dependency which is missing from the container.Equivalence
Give the limitations with MEF, I'd recommend something else like Unity, unless you want to design an application with pluggable modules. This is what MEF is designed for.Equivalence
@Contango: There is a debugging tool for mef called MEFX: mef.codeplex.com/releases/view/33536, which can be integrated to your application. It shows which classes satisfied which dependencies and similar information.Charcuterie
MEFX is now on MEF Analysis Tool (mefx) for .NET 4.0 Beta and even have docs.Ascertain
Using Microsoft.ComponentModel.Composition.Diagnostics might be better than MEFX in situations where *.exe.config and *.dll.config files are used which MEFX doesn't support. Also the Trace window of Visual Studio can be used to debug. learn.microsoft.com/en-us/archive/msdn-magazine/2010/february/…Thermonuclear

© 2022 - 2024 — McMap. All rights reserved.