Reputation: 4240
I am using MEF to create a plugin style architecture, but Im getting a composition exception.
Here is the detail.
I have the following code:
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))
{
}
}
On the line:
if (!this.pluginDictionary.ContainsKey(plugin.Value.ModuleName))
I get the following exception thrown:
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.
This was all working then I changed my plugin to inherit from the System.Windows.Window class. Could this be causing the failure?
Heres the plugin header:
[Export(typeof(IGX3PluginInterface))]
public partial class MainWindow : GX3ClientPlugins.GX3ClientPlugin
Where GX3ClientPlugin extends the System.Windows.Window class.
Im pretty sure this is relevant but dont full yunderstand it :) http://mef.codeplex.com/discussions/81717
Please let me know what other information you require?
Upvotes: 0
Views: 1877
Reputation: 66723
The exception is not really MEF-related, but is thrown by the constructor of a WPF Window when MEF tries to create it. As the exception states, WPF windows don't like to be created on a non-STA thread.
I can think of two different reasons why you might be getting this error:
1) Your main thread is not an STA-thread, because the Main
entry point of your application doesn't have the STAThread attribute.
2) You are invoking ComposeParts
on another thread which is not the main thread, and this other thread was created without setting the apartment state to ApartmentState.STA
.
See also this other question.
Upvotes: 2