Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/// <summary>
/// Static helper class to debug failed MEF composition.
/// </summary>
public static class MefDebuggingHelper
{
/// <summary>
/// Writes missing import definitions to the debug console.
/// </summary>
/// <param name="container"></param>
[Conditional("DEBUG")]
public static void PrintMissingImports(CompositionContainer container)
{
var missingImports = GetMissingImports( container );
if( missingImports.Count == 0 )
{
return;
}
Debug.WriteLine( string.Empty );
Debug.WriteLine( "=============== Missing Imports ===============" );
foreach( var missing in missingImports )
{
Debug.WriteLine( missing );
}
Debug.WriteLine( "===============================================" );
Debug.WriteLine( string.Empty );
}
private static List<string> GetMissingImports(CompositionContainer container )
{
var brokenExports = GetBrokenExports(container);
return container.Catalog
.SelectMany( part => part.ImportDefinitions )
.Where( impDef => IsInvalidImport(container, impDef) )
.Select( impDef => impDef.ContractName )
.Distinct()
.Where( contract => !contract.Contains( "ExportFactory" ) )
.Where( contract => !brokenExports.Contains( contract ) )
.OrderBy( contract => contract )
.ToList();
}
private static List<string> GetBrokenExports(CompositionContainer container)
{
return container.Catalog
.Where( part => part.ImportDefinitions.Any( impDef => IsInvalidImport(container, impDef) ) )
.SelectMany( part => part.ExportDefinitions )
.Select( exDef => exDef.ContractName )
.Distinct()
.OrderBy( c => c )
.ToList();
}
private static bool IsInvalidImport(CompositionContainer container, ImportDefinition impDef )
{
try
{
container.GetExports( impDef );
}
catch
{
return true;
}
return false;
}
}
@igitur

This comment has been minimized.

Copy link

@igitur igitur commented Oct 27, 2020

Oh, man, thanks! I was struggling so much with failed imports. End the end due to a typo. But MEF really doesn't make it easy to trace the issue.

@VacuumBreather

This comment has been minimized.

Copy link
Owner Author

@VacuumBreather VacuumBreather commented Oct 27, 2020

Oh, man, thanks! I was struggling so much with failed imports. End the end due to a typo. But MEF really doesn't make it easy to trace the issue.

You are very welcome. I'm glad it helped.

@aubinchedotal-bib

This comment has been minimized.

Copy link

@aubinchedotal-bib aubinchedotal-bib commented Nov 4, 2020

Thank you for that !
How/Where would you call PrintMissingError to display the error behind a failed viewmodel instanciation ?

@VacuumBreather

This comment has been minimized.

Copy link
Owner Author

@VacuumBreather VacuumBreather commented Nov 4, 2020

Thank you for that !
How/Where would you call PrintMissingError to display the error behind a failed viewmodel instanciation ?

In the bootstrapper after the MEF container has been built.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment