TypeLoadException with DirectoryCatalog

Sep 24, 2009 at 12:30 AM

Hi everyone,

When I create my Catalog with :

DirectoryCatalog catalog = new DirectoryCatalog("File Formats", "*Input.dll");

A TypeLoadException is thrown because MEF tries to load a missing type. But, why does MEF search a type which is NOT ANYWHERE in my code?

The InputFileAttribute seems to be a ghost reference.

If someone could help me...

All the MEF code I use:

// in the host

[ImportMany(typeof(IMetatoggerFileFormat))]
public Lazy<IMetatoggerFileFormat, IFileFormat>[] FileFormats { get; set; }

private void LoadAudioFormats() // called in the constructor of my main WPF window
      {
         DirectoryCatalog catalog = new DirectoryCatalog("File Formats", "*Input.dll");
         CompositionContainer container = new CompositionContainer(catalog);
         CompositionBatch batch = new CompositionBatch();
         batch.AddPart(this);
         container.Compose(batch);
      }

 

// in the plugin

[Export(typeof(IMetatoggerFileFormat))]
   [FileFormat("Free Lossless Audio Codec (*.flac)", "*.flac")]
   [PartCreationPolicy(CreationPolicy.NonShared)]
   public class Flac : IMetatoggerFileFormat
   {

//

}

// in the shared assembly

public interface IMetatoggerFileFormat
   {
      string Codec { get; }    
   }

   public interface IFileFormat
   {
      string[] Extension { get; }
      string[] File { get; }
   }

   [MetadataAttribute]
   [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
   public sealed class FileFormat : Attribute
   {
      public FileFormat(string file, string extension)
      {
         File = file;
         Extension = extension.ToLowerInvariant();
      }

      public string Extension { get; private set; }
      public string File { get; private set; }
   }

Here is the stack trace of the exception:

System.TypeLoadException
            Message="Impossible de charger le type 'MetatOGGer.Plugins.InputFileAttribute' à partir de l'assembly 'MetatOGGer.Plugins, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'."
            Source="mscorlib"
            TypeName="MetatOGGer.Plugins.InputFileAttribute"
            StackTrace:
                 à System.ModuleHandle.ResolveType(Int32 typeToken, RuntimeTypeHandle* typeInstArgs, Int32 typeInstCount, RuntimeTypeHandle* methodInstArgs, Int32 methodInstCount)
                 à System.ModuleHandle.ResolveTypeHandle(Int32 typeToken, RuntimeTypeHandle[] typeInstantiationContext, RuntimeTypeHandle[] methodInstantiationContext)
                 à System.Reflection.Module.ResolveType(Int32 metadataToken, Type[] genericTypeArguments, Type[] genericMethodArguments)
                 à System.Reflection.CustomAttribute.FilterCustomAttributeRecord(CustomAttributeRecord caRecord, MetadataImport scope, Assembly& lastAptcaOkAssembly, Module decoratedModule, MetadataToken decoratedToken, RuntimeType attributeFilterType, Boolean mustBeInheritable, Object[] attributes, IList derivedAttributes, RuntimeType& attributeType, RuntimeMethodHandle& ctor, Boolean& ctorHasParameters, Boolean& isVarArg)
                 à System.Reflection.CustomAttribute.IsCustomAttributeDefined(Module decoratedModule, Int32 decoratedMetadataToken, RuntimeType attributeFilterType, Boolean mustBeInheritable)
                 à System.Reflection.CustomAttribute.IsDefined(RuntimeType type, RuntimeType caType, Boolean inherit)
                 à System.RuntimeType.IsDefined(Type attributeType, Boolean inherit)
                 à Microsoft.Internal.AttributeServices.IsAttributeDefined[T](ICustomAttributeProvider attributeProvider) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\Microsoft\Internal\AttributeServices.cs:ligne 35
                 à System.ComponentModel.Composition.AttributedModel.AttributedPartCreationInfo.IsPartDiscoverable() dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\AttributedModel\AttributedPartCreationInfo.cs:ligne 82
                 à System.ComponentModel.Composition.AttributedModel.AttributedModelDiscovery.CreatePartDefinitionIfDiscoverable(Type type, ICompositionElement origin) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\AttributedModel\AttributedModelDiscovery.cs:ligne 21
                 à System.ComponentModel.Composition.Hosting.TypeCatalog.get_PartsInternal() dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\TypeCatalog.cs:ligne 163
                 à System.ComponentModel.Composition.Hosting.TypeCatalog.CreateIndex() dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\TypeCatalog.cs:ligne 260
                 à System.Lazy`1.get_Value() dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\Lazy.cs:ligne 61
                 à System.ComponentModel.Composition.Hosting.TypeCatalog.GetCandidateParts(ImportDefinition definition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\TypeCatalog.cs:ligne 246
                 à System.ComponentModel.Composition.Hosting.TypeCatalog.GetExports(ImportDefinition definition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\TypeCatalog.cs:ligne 214
                 à System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetExports(ImportDefinition definition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\AssemblyCatalog.cs:ligne 168
                 à System.ComponentModel.Composition.Hosting.DirectoryCatalog.<>c__DisplayClass1.<GetExports>b__0(ComposablePartCatalog catalog) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\DirectoryCatalog.cs:ligne 258
                 à System.Linq.Enumerable.<SelectManyIterator>d__14`2.MoveNext()
                 à System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\CatalogExportProvider.cs:ligne 246
                 à System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ExportProvider.cs:ligne 208
                 à System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ExportProvider.cs:ligne 102
                 à System.ComponentModel.Composition.Hosting.AggregateExportProvider.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\AggregateExportProvider.cs:ligne 157
                 à System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ExportProvider.cs:ligne 208
                 à System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExports(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ExportProvider.cs:ligne 145
                 à System.ComponentModel.Composition.Hosting.CompositionContainer.GetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\CompositionContainer.cs:ligne 396
                 à System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(ImportDefinition definition, AtomicComposition atomicComposition, IEnumerable`1& exports) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ExportProvider.cs:ligne 208
                 à System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(ImportDefinition definition, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ExportProvider.cs:ligne 102
                 à System.ComponentModel.Composition.Hosting.ImportEngine.TryGetExports(ExportProvider provider, ComposablePart part, ImportDefinition definition, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ImportEngine.cs:ligne 721
                 à System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportSubset(PartManager partManager, IEnumerable`1 imports, AtomicComposition atomicComposition) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ImportEngine.cs:ligne 451
                 à System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportsStateMachine(PartManager partManager, ComposablePart part) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ImportEngine.cs:ligne 345
                 à System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImports(PartManager partManager, ComposablePart part, Boolean shouldTrackImports) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ImportEngine.cs:ligne 423
                 à System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImports(ComposablePart part) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ImportEngine.cs:ligne 159
                 à System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.<>c__DisplayClass2.<Compose>b__0() dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ComposablePartExportProvider.cs:ligne 277
                 à System.ComponentModel.Composition.Hosting.CompositionServices.TryInvoke(Action action) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\CompositionServices.cs:ligne 440
                 à System.ComponentModel.Composition.Hosting.ComposablePartExportProvider.Compose(CompositionBatch batch) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\ComposablePartExportProvider.cs:ligne 276
                 à System.ComponentModel.Composition.Hosting.CompositionContainer.Compose(CompositionBatch batch) dans F:\Work\Beta\MEF_PreBeta_2\src\ComponentModel\System\ComponentModel\Composition\Hosting\CompositionContainer.cs:ligne 207
                 à MetatOGGer.Windows.OutputWindow.LoadAudioFormats() dans H:\Personnel\Développement\Toolkit\Laboratoire\MEF\MetatOGGer\MetatOGGer\Windows\OutputWindow.xaml.cs:ligne 101
                 à MetatOGGer.Windows.OutputWindow..ctor() dans H:\Personnel\Développement\Toolkit\Laboratoire\MEF\MetatOGGer\MetatOGGer\Windows\OutputWindow.xaml.cs:ligne 90

Developer
Sep 24, 2009 at 5:16 PM

Any ideas where InputFileAttribute lives? Or what it even is?

Sometimes these things happen when some assembly is outdated. Perhaps you used to have InputFileAttribute in one assembly but it doesn't exist anymore but some assembly still references it. Try rebuilding and recopying the assemblies to your directory.

Sep 24, 2009 at 6:04 PM

I tried to rebuild my solution many times but with no effect.

To resolve this issue, I have to change the platform solution from x86 to "Any CPU", rebuild (my plugin assembly generating BadImageException when MEF loading it), then change back from "Any CPU" to x86.

After that, everything works.

It was very strange.

If you have an explication?

Best regards