2
votes

I'm trying to use Microsoft.Build.Evaluation.Project to run MSBuild inside my project.

I'm getting this error, and I can't figure out why, or where to start diagnosing it:

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\Microsoft.CodeAnalysis.targets: error MSB4127: The "CodeAnalysis" task could not be isntantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\CodeAnalysis\.\FxCopTask.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.Build.Tasks.CodeAnalysis' to type 'Microsoft.Build.Framework.ITask'.

  • I've tried using ToolsVersion="12.0" in my build file
  • I've checked Microsoft.CodeAnalysis.targets and ensured FxCopTask.dll works
  • I've removed extraneous references and am now only referencing Microsoft.Build and Microsoft.Build.Framework
  • I've used MSBuild.exe in a shell process successfully on the same solution
  • This question seems to address the problem, but it's using the command line.

I'm not sure how to "verify the task assembly" or add "a binding redirect for the Microsoft.Build.Framework." Can someone point me in the right direction?

2

2 Answers

4
votes

The answer here lay in obfuscated references. The default Microsoft.Build references are v4.0.

Turns out to reference the newest versions (at least in VS 2013 Community) I had to browse to the assemblies in C:\Program Files (x86)\MSBuild\12.0\bin and manually add the references. Success!

Thanks to BuildManager to use another version of MSBuild for pointing out this solution.

1
votes

'Adding a binding redirect' is adding the following to your app.config file:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="12.0.0.0"/> <!--verify version!-->
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

For me, this resolved the issue. My error started with The "csc" task could not be instantiated, though.