Import Cardinality Mismatch Exception

Jan 4, 2011 at 3:57 PM

I am learning MEF and trying to do a sample app modeled after the MVVM RI app that comes with Prism 4.0.

I am using RIA domain services to access a sql database.

I have run into an issue where I get an ImportCardinalityMismatchException. I have used Visual MEFX but it finds the related export. This is the piece of code that throws the exception:

private T GetInstance<T>()
        {
            return this.container.GetExportedValue<T>();
        }
This is the exception I get:
No valid exports were found that match the constraint '((exportDefinition.ContractName == "Common.Infrastructure.ICurrentState(SilverlightWCFRIALibrary.Web.Role)")
AndAlso (exportDefinition.Metadata.ContainsKey("ExportTypeIdentity") 
AndAlso "Common.Infrastructure.ICurrentState(SilverlightWCFRIALibrary.Web.Role)".Equals(exportDefinition.Metadata.get_Item("ExportTypeIdentity"))))', 
invalid exports may have been rejected.
This is where the export has been created:
 public class RoleCurrentState : CurrentState<Role>
    {
    }
/// <summary>
    /// Generic implementation for the <see cref="ICurrentState{T}"/> interface.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class CurrentState<T> : ICurrentState<T>
    {
        /// <summary>
        /// Gets or sets the current value.
        /// </summary>
        public T Value { get; set; }
    }
 [InheritedExport]
    public interface ICurrentState<T>
    {
        /// <summary>
        /// Gets and sets the current value.
        /// </summary>
        T Value { get; set; }
    }
Any ideas?
Jan 6, 2011 at 10:06 PM

This was happening because of misnamed namespaces.

Jan 12, 2011 at 11:18 AM

Can you explan you solution more and perhaps some of the tecniques you used to find the offending item.  I have having the same issue and am very frustred (See my post from this weekend).

Jan 20, 2011 at 10:17 AM

I've been having the same issue, I got it fixed by adding the correct assembly to the catalog. In my example, I had to add the main assembly from the Bootstrapper class:

public class Bootstrapper : MefBootstrapper
{
    protected override void ConfigureAggregateCatalog()
    {
        base.ConfigureAggregateCatalog();
        AggregateCatalog.Catalogs.Add(new AssemblyCatalog(typeof(Bootstrapper).Assembly));
    }
    
    ...
    
}
Hope this helps :)