4

I am trying to perform a build on a WPF (.Net Framework 4.0) project using the Microsoft.Build assemblies, i.e. not building from VS and not building using stock standard MSBuild from command line. All my projects build successfully, but the WPF project fails with the following message:

C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\Microsoft.WinFx.targets(268,9): error MSB4127: The "MarkupCompilePass1" task could not be instantiated from the assembly "PresentationBuildTasks, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". 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.Windows.MarkupCompilePass1' to type 'Microsoft.Build.Framework.ITask'. [C:\Service\Test.csproj] C:\WINDOWS\Microsoft.NET\Framework64\v4.0.30319\Microsoft.WinFx.targets(268,9): error MSB4060: The "MarkupCompilePass1" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.

I have found references (on Stack Overflow) mentioning the updated MSBuild assemblies (12.0 vs 4.0) etc etc. This has all been updated, i.e. references from the build utility, but no luck.

Any ideas/suggestions?

NubieJ
  • 575
  • 2
  • 9
  • 21

2 Answers2

3

We are using a similar system and the problem seems to stem from the compilation of the Page tag: <generator>MSBuild:Compile</generator>. This seems to be invoking MSBuild in a way that pulls the 4.0 framework libraries.

For our build executable, we simply modified the configuration to include the configurations used by the 12.0 MSBuild configuration. i.e., take the elements in C:\Program Files (x86)\MSBuild\12.0\bin\MSBuild.exe.config and place them in your application configuration. This resolved the issues for us.

Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
NycWes
  • 31
  • 2
  • 2
    I'm having the exact same problem as the OP. Can you clarify more specifically what your modifications were? I was able to find the config file you mentioned as well as the MSBuild:Compile lines, but I'm not sure how to merge the two files. – Brent Jul 28 '16 at 22:16
  • 1
    Whoa, you saved my day. @Brent In my case, the 'build executable' is an ASP.NET project with a Web.config which has a section that looks like " " - under this section, paste the dependent assembly xml in C:\Program Files (x86)\MSBuild\14.0\Bin – tofutim Sep 20 '16 at 23:40
  • Thanks @tofutim ! That clarified things for me and I got it working just now! – Brent Sep 27 '16 at 17:04
  • Actually false alarm, I still have the same problem as before. I'm trying to get this to work with Costura/Fody. – Brent Sep 27 '16 at 17:21
2

I found a solution. Add a binding redirect to the version you want to use in your App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <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="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build.Framework" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Build" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
        <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Suplanus
  • 1,523
  • 16
  • 30