Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MEF component with [Export] in VS extension - No exports found that match the constraint

In a Visual Studio extension I'm trying to export a MEF component and find it later.

I have this class:

[Export(typeof(IBoilerplateSettings))]
public class BoilerplateSettings : IBoilerplateSettings
{
    ...

And this code to resolve in a callback from an OleMenuCommand. Got example from this pull request.

var componentModel = (IComponentModel)this.package.GetService(typeof(SComponentModel));
var settings = componentModel.DefaultExportProvider.GetExportedValue<IBoilerplateSettings>();

But it throws this error every time:

No exports were found that match the constraint: 
    ContractName    UnitTestBoilerplate.IBoilerplateSettings
    RequiredTypeIdentity    UnitTestBoilerplate.IBoilerplateSettings

Both are in the same assembly. I've tried the [Export] attribute in both System.Composition and System.ComponentModel.Composition but neither work.

I'm looking at the docs for MEF in VS but it sounds like adding the attribute should just work. I've also tried clearing the ComponentModel cache, but that didn't work. What am I missing here?

like image 855
RandomEngy Avatar asked Oct 19 '25 04:10

RandomEngy


1 Answers

Generally two things to check:

  1. Make sure your project is mentioned as being a MEF component inside your .vsixmanifest. If it's not, VS won't be looking in your assembly.
  2. If you're on Visual Studio 2015 or later, in the ComponentModelCache folder there's a .err file that lists errors when creating the MEF composition. Make sure something you care about isn't in there. (It's normal to have crap in there...we aren't always the best about shipping that in a clean state.)

And if that doesn't work, there's often the aggressive method which is to just break on all exceptions when Visual Studio is running, and see if you can find anything related to your extension. You might find some other loading exception, or some other root cause. Unfortunately that's a very scattershot approach.

like image 107
Jason Malinowski Avatar answered Oct 20 '25 18:10

Jason Malinowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!