Search code examples
c#visual-studio-2015roslynvsix

Roslyn in a VSIX project: Unable to cast object of type ComponentModel to IComponentModel


I've been trying out Roslyn in a VSIX project. At the moment what I want to do is load in the CurrentSolution. However I end up with the error:

Unable to cast object of type 'Microsoft.VisualStudio.ComponentModelHost.ComponentModel' 
to type 'Microsoft.VisualStudio.ComponentModelHost.IComponentModel'.

I've installed the NuGet packages for Microsoft.VisualStudio.LanguageServices and Microsoft.VisualStudio.ComponentModelHost.

In the Initialize method of my VSIX command package class, I've added this:

var componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
var workspace = (Workspace)componentModel.GetService<VisualStudioWorkspace>();

That gives me the exception I mentioned.

The version of the Microsoft.VisualStudio.ComponentModelHost DLL is 15.0.0.0.

Already tried:

  • Removing BIN folder and rebuilding

  • Restarting Visual Studio

I'm just not sure what's causing this and can't seem to find information about this error, some help would be greatly appreciated.


Solution

  • Got the idea to check the GAC for versions of this DLL. Turns out I had two. Via the Nuget package manager, I installed version 15.0.0.0 of the DLL. But I already had a 14.0.0.0 version (not sure where that came from). I removed 15.0.0.0 from the GAC and from the references of my project, then added the existing 14.0.0.0 reference. Casts without a problem now.