Roslyn: MSBuildWorkspace: Exception : Unable to cast object of type 'Microsoft.CodeAnalysis.BuildTasks.Csc' to type 'Microsoft.Build.Framework.ITask'.

Created on 7 Feb 2018  路  6Comments  路  Source: dotnet/roslyn

Version Used: 2.6.1

Steps to Reproduce:

  1. Create console app using VS 2015 and add nuget reference to Microsoft.CodeAnalysis (.NET framework 4.6.1)
  2. On the same machine, VS 2017 comunity version is also installed.
  3. Try to call below program:
using (var workspace = MSBuildWorkspace.Create())
{
     Project currentProject = workspace.OpenProjectAsync(projectPath).Result;
     workspace.LoadMetadataForReferencedProjects = true;
 }

here the projectPath is a path of .csproj file.

Expected Behavior:
workspace.OpenProjectAsync(projectPath).Result; should run successfully and open the project.

Actual Behavior:
Unable to cast object of type 'Microsoft.CodeAnalysis.BuildTasks.Csc' to type 'Microsoft.Build.Framework.ITask'.
Stack Trace:
at Microsoft.Build.Shared.TaskLoader.CreateTask(LoadedType loadedType, String taskName, String taskLocation, Int32 taskLine, Int32 taskColumn, LogError logError, AppDomainSetup appDomainSetup, Boolean isOutOfProc, AppDomain& taskAppDomain)

Additional Info:

Following are the nuget packages refereed

<package id="Microsoft.Build" version="15.5.180" targetFramework="net461" />
  <package id="Microsoft.Build.Framework" version="15.5.180" targetFramework="net461" />
  <package id="Microsoft.Build.Tasks.Core" version="15.5.180" targetFramework="net461" />
  <package id="Microsoft.Build.Utilities.Core" version="15.5.180" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.Analyzers" version="1.1.0" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.Common" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.CSharp" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.CSharp.Features" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.CSharp.Workspaces" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.Features" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.VisualBasic" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.VisualBasic.Features" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.CodeAnalysis.Workspaces.Common" version="2.6.1" targetFramework="net461" />
  <package id="Microsoft.Net.Compilers" version="2.6.1" targetFramework="net461" developmentDependency="true" />
Area-IDE Bug

Most helpful comment

FWIW, the way to fix this is not really obvious. Essentially, the problem is that your application includes versions of the Microsoft.Build.* assemblies that are not compatible with the tasks that the project being loaded use. Unfortunately, this problem is exacerbated by the fact that the Microsoft.CodeAnalysis.Workspaces.Common package (which includes the MSBuildWorkspace type) pulls those packages and assemblies into your project.

If you want to get this working, there are two things to do:

  1. Delete the Microsoft.Build.* assemblies from your build output.
  2. Use the Microsoft.Build.Locator NuGet package to find and register an instance of MSBuild for MSBuildWorkspace to use.

This has the effect of loading the Microsoft.Build.* assemblies that MSBuildWorkspace needs from the registered instance of MSBuild. These should be compatible with the tasks that are used by the project you're trying to load.

Take a look at my reply here for more detail: https://github.com/dotnet/roslyn/issues/26029#issuecomment-380164421.

Also, in that issue, the customer created a GitHub repo demonstrating the problem. I went ahead and submitted a PR that shows how to fix the issue in that repo here: https://github.com/FizzerWL/ErrorRepro/pull/1. I hope that PR will be instructive to you too.

All 6 comments

Hi,

I'm getting the same issue. is there any information if this issue will be fixed.

thanks,

Abdelkader.

FWIW, the way to fix this is not really obvious. Essentially, the problem is that your application includes versions of the Microsoft.Build.* assemblies that are not compatible with the tasks that the project being loaded use. Unfortunately, this problem is exacerbated by the fact that the Microsoft.CodeAnalysis.Workspaces.Common package (which includes the MSBuildWorkspace type) pulls those packages and assemblies into your project.

If you want to get this working, there are two things to do:

  1. Delete the Microsoft.Build.* assemblies from your build output.
  2. Use the Microsoft.Build.Locator NuGet package to find and register an instance of MSBuild for MSBuildWorkspace to use.

This has the effect of loading the Microsoft.Build.* assemblies that MSBuildWorkspace needs from the registered instance of MSBuild. These should be compatible with the tasks that are used by the project you're trying to load.

Take a look at my reply here for more detail: https://github.com/dotnet/roslyn/issues/26029#issuecomment-380164421.

Also, in that issue, the customer created a GitHub repo demonstrating the problem. I went ahead and submitted a PR that shows how to fix the issue in that repo here: https://github.com/FizzerWL/ErrorRepro/pull/1. I hope that PR will be instructive to you too.

@DustinCampbell's fix did not entirely work for me - even after doing what he suggested, ReflectionTypeLoadExceptions were being thrown because Microsoft.CodeAnalysis.Workspaces.Desktop 2.7.0.0 was trying to load Microsoft.Build.Framework, Microsoft.Build, and Microsoft.Build.Tasks.Core versions 15.1.0.0, but the versions in my Visual Studio installation directory were 15.8.65.37198.

Th additional step to make it all work was to add bindingRedirects to point to the versions of Microsoft.Build.* from my Visual Studio install.

To be honest, this whole process is terrible and messy and painful and totally bloody unnecessary; even finding this issue was a chore in and of itself. This is NOT the experience I'm used to with Microsoft tools, and it's extremely disappointing. When will this hoop-jumping be fixed?

Hey @IanKemp: I'd love to dig in a bit more with you to figure out what happened. The 15.8.65.347198 versions that you mentioned are _file_ versions. The assembly versions (which binding redirects work against) are 15.1.0.0 according to IL Sky.

I _suspect_ the problem that you encountered was calling MSBuildLocator.RegsterDefaults() after accessing MSBuildWorkspace. This needs to be called _before_ any accesses to MSBuildWorkspace or the MEF composition in MSBuildWorkspace won't compose properly (since it requires types from Microsoft.Build.*. Could you check this and see if that was the problem you encountered? If not, would it be possible to share your code?

I had the same issues and was able to reproduce/resolve the problem in this project: sample-project - see commit history

Adding BindingRedirects and the Microsoft.Build.Locator nuget pacakge as stated above by @DustinCampbell resolved my issues

Issue was reported resolved.

Was this page helpful?
0 / 5 - 0 ratings