Have you ever received the following exception while using MEF?
Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.
if you have received this error did you scratch your head and wonder ‘what the F does this mean’? Well the long and short of it is this. This means that while trying to load an Export there was a dependency which could not be found during reflection.
The quick way to determine the cause of the error is to do as the image below shows:
What I would suggest is to wrap your MEF logic inside of a try-catch and explicitly catch ReflectionTypeLoadException. Inside of your catch add some logic as such (just a PoC, copy-paster beware).
catch (ReflectionTypeLoadException tLException)
{
var loaderMessages = new StringBuilder();
loaderMessages.AppendLine("While trying to load composable parts the follwing loader exceptions were found: ");
foreach (var loaderException in tLException.LoaderExceptions)
{
loaderMessages.AppendLine(loaderException.Message);
}
// this is one of our custom exception types.
throw new PluginLoadingException(loaderMessages.ToString(), tLException);
}
As you can see when you look at the LoaderExcpetions collections from my image above I had forgotten to reference AutoMapper in my plug-in and all hell broke lose (btw, no idea how this even compiled, but that is a different post).
Hope this helps.
Till next time,
Posted
01-31-2010 8:51 AM
by
Derik Whittaker