roslyn: System.Reflection.ReflectionTypeLoadException when calling Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create().OpenProjectAsync

Version Used: Visual Studio 2017 RC4 Microsoft.CodeAnalysis NuGet package 2.0.0-rc4

Steps to Reproduce:

  1. Create new WPF app (.NET 4.6.1)
  2. Add CodeAnalysis package from NuGet
  3. Add code (I did within an async Button Click event.
Project project = await Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create().OpenProjectAsync(@"C:\Projects\WpfApp4\WpfApp4\WpfApp4.csproj");
  1. Run/debug code.

Expected Behavior: Should be able to load a Project object.

Actual Behavior: Get the following error.

System.Reflection.ReflectionTypeLoadException occurred HResult=0x80131602 Message=Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information. Source=mscorlib StackTrace: at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) at System.Reflection.RuntimeAssembly.get_DefinedTypes() at System.Composition.Hosting.ContainerConfiguration.<WithAssemblies>b__0(Assembly a) at System.Linq.Enumerable.<SelectManyIterator>d__162.MoveNext() at System.Composition.TypedParts.TypedPartExportDescriptorProvider..ctor(IEnumerable1 types, AttributedModelProvider attributeContext) at System.Composition.Hosting.ContainerConfiguration.CreateContainer() at Microsoft.CodeAnalysis.Host.Mef.MefHostServices.Create(IEnumerable`1 assemblies) at Microsoft.CodeAnalysis.Host.Mef.DesktopMefHostServices.get_DefaultServices() at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.Create() at WpfApp4.MainWindow.<Test_OnClick>d__2.MoveNext() in c:\Projects\WpfApp4\WpfApp4\MainWindow.xaml.cs:line 38

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 7
  • Comments: 27 (8 by maintainers)

Most upvoted comments

OK, it looks like I got it to work, but had to download a few other NuGet packages.

Microsoft.Build Microsoft.Build.Framework Microsoft.Build.Tasks.Core

I guess my question now is why would these not already be included or not pulled down when getting Microsoft.CodeAnalysis? Is it because of VS RC and RC packages?

Had this issue in an empty project(in Rider IDE) with nuget CodeAnalysis 3.3.1. Solved by a simple call MSBuildLocator.RegisterDefaults(); before creating workspace.

Still get this with VS15.5 Preview 4. Must reference the following Nugets…

Microsoft.Build Microsoft.Build.Framework Microsoft.Build.Tasks.Core

That sounds like your .exe.config needs to have BindingRedirects in place to redirect the msbuild task assemblies to the version of msbuild you are using.

Yes, this is related to https://github.com/dotnet/roslyn/issues/15056, but we should probably add dependencies to our packages now. Tagging @mattwar