2012-05-03 47 views
0

我正在使用MEF創建插件樣式體系結構,但是我得到了組合異常。無法從部分導出

這裏是細節。

我有以下代碼:

AggregateCatalog catalog = new AggregateCatalog(); 
catalog.Catalogs.Add(new DirectoryCatalog(pluginDirectory)); 
CompositionContainer container = new CompositionContainer(catalog); 
container.ComposeParts(this); 

// add to dictionary 
foreach (Lazy<IGX3PluginInterface> plugin in plugins) 
{ 
    if (!this.pluginDictionary.ContainsKey(plugin.Value.ModuleName)) 
    { 
    } 
} 

就行了:

if (!this.pluginDictionary.ContainsKey(plugin.Value.ModuleName)) 

我得到下面的異常拋出:

Exception = {"The composition produced a single composition error. The root cause is provided below. Review the CompositionException.Errors property for more detailed information. 1) The calling thread must be STA, because many UI components require this.

這是所有工作的話,我改變了我插件從System.Windows.Window類繼承。這是否會導致失敗?

赫雷什插件頭:

[Export(typeof(IGX3PluginInterface))] 
public partial class MainWindow : GX3ClientPlugins.GX3ClientPlugin 

凡GX3ClientPlugin延伸System.Windows.Window類。

Im相當肯定這是相關但不完全yunderstand吧:) http://mef.codeplex.com/discussions/81717

請讓我知道你需要什麼其他信息?

回答

2

這個異常不是真的與MEF相關,而是在MEF嘗試創建它時由WPF窗口的構造函數拋出。作爲例外狀態,WPF窗口不希望在非STA線程上創建。

我能想到的兩個不同的原因,你可能會得到這樣的錯誤:

1)你的主線程不是STA線程,因爲應用程序的Main入口點不具有STAThread屬性。

2)您在另一個不是主線程的線程上調用ComposeParts,而另一個線程是在沒有setting the apartment stateApartmentState.STA的情況下創建的。

另請參閱此other question

+0

謝謝你會檢查這些並回復給你 – user589195