MEF and strong-named assemblies

Jul 18, 2011 at 8:04 PM

I created a strong-named assembly that would be added to the "Extensions" folder where MEF would attempt to load assemblies. I was expecting that if I edited the strong-named assembly, i.e., corrupted it, that MEF would fail to load the assembly. However, that was not the case! It merrily loaded the assembly. Am I missing something here? I verified that the assembly was strong-named at load time via the following code:

                var catalog = new AggregateCatalog();
                directoryCatalog = new DirectoryCatalog("Extensions");

                AssemblyName assemblyName = null;
                foreach (var file in directoryCatalog.LoadedFiles)
                    assemblyName = AssemblyName.GetAssemblyName(file);
                    if (assemblyName.GetPublicKey().SequenceEqual(publicKey))

                container = new CompositionContainer(catalog);

                // Resolve the dependencies here.

Aug 1, 2011 at 6:23 PM

MEF delegates any assembly validation to the CLR loader - eventually we will call Assembly.Load() on your assembly. The quirk of the DirectoryCatalog is that it tries to address deployment fragilty (e.g. invalid assemblies, native dlls etc) by basically ignoring the exception in question and moving on (if you enable MEF tracing, you will see those failures appear in the trace though). So it's quite possible that the assembly in question might not have actually been loaded.

The most reliable way to establish that is to look at catalog.Parts and see if parts from the assembly in question found its way in it.
You also want to make sure that string name validation for the given public key was not turned off, because that would stop CLR from even attemption the strong name validation.

Hope this helps