Instantiating a new type with MEF

314 views Asked by At

I am using CompositionInitializer.SatisfyImports(this) from Glen Block with a WPF application using Prism 4.1 and Prism's MEFExtensions.

I have used this many times before and not had a problem, but every once in a while when I call SatisfyImports(this) and get the following error:

A first chance exception of type 'System.Resources.MissingManifestResourceException' occurred in mscorlib.dll Additional information: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "System.ComponentModel.Composition.Initialization.Strings.resources" was correctly embedded or linked into assembly "Microsoft.ComponentModel.Composition.Initialization.Desktop" at compile time, or that all the satellite assemblies required are loadable and fully signed. If there is a handler for this exception, the program may be safely continued.

Anyone know why I'm getting this error and how to fix it?

1

There are 1 answers

0
Nicros On

The CompositionInitializer approach doesn't work with WPF in some cases, as evidenced by the MissingManifestResourceException above- basically, I think this is saying that one of the imports is failing due to some resource problem of the imported DLL... which you may or may not have any control over.

The better approach (I think) is to use the MEF ExportFactory to instantiate objects for WPF applications rather than the CompositionInitializer:

[Export]
public class OrderController {

  [Import] 
  public ExportFactory<OrderViewModel> OrderVMFactory {get;set;}

  public OrderViewModel CreateOrder() {
    return OrderVMFactory.CreateExport().Value;
  }
}

Worked for me anyways.

I do have some questions about memory management and the export factory, but that's another post :)