Created
April 13, 2020 06:44
-
-
Save VacuumBreather/5be8970f9e5f86e1c912ef0ea14c786e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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; | |
} | |
} |
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.
Thank you for that !
How/Where would you call PrintMissingError to display the error behind a failed viewmodel instanciation ?
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
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.