2014-03-27 47 views
1

我有一個有很多MEF的應用程序,它一直導入。在調試器中,如果找不到零件,我可以看到一個ImportCardinalityMismatchException被引發,告訴我到底是什麼缺少的導入。但隨後它被吞噬了,我是由MEF承擔的,並且最終會彈出一條日誌消息,告訴我關於一個不同的組件(可能間接地)依賴於失敗的組件。是否有可能在調試器之外獲得原始故障?我可以防止吞下ImportCardinalityMismatchException嗎?

回答

0

一種解決方案是使用上的參數[Import(AllowDefault = true)]屬性進口構造,然後處理/登錄自己的失敗在構造函數體:

[ImportingConstructor] 
    public SomeView([Import(AllowDefault = true)] SomeViewModel context) 
    { 
     if (context == null) 
     { 
      LogManager.GetLogger("Composition").Error("No object found to satisfy import of 'SomeViewModel'"); 
      throw new ArgumetnNullException ("SomeViewModel"); 
     } 
相關問題