Search code examples
visual-studiomefvspackage

Importing MEF Components from VSPackage Class


I need to be able to communicate with my MEF component from my package class. I tried several different approaches:

used [import] attributes from the package class. However the properties are always null. As I found out this is due to the fact that you cannot import MEF components from the package class.

Tried calling "componentModel.DefaultExportProvider.GetExportedValue" to get an instance of a class created by MEF. This did actually return an instance, but it appears to be a copy, or something that is not linked to the actual MEF Component

The MEF Component I'm trying to access is a custom Team Explorer page section that I created. My package has an event that when triggered, will need to call into this MEF Component via method call with some information about my application state.

It seems like this should be straightforward but I've been stuck for days.

I simply need to communicate with an MEF component from a package. Can I do this? Is this at all possible?


Solution

  • I found the answer. For anyone interested refer to SLaks' posted example solution:Rebracer

    The basic idea is that the Package class cannot create anything directly, not even OLEMenuCommands. All of that needs to be instantiated implicitly by visual studio by using MEF.

    How to register my service as a global service or how can I use MEF in my scenario?