Building WPF application using Microsoft.Build assemblies

1.8k views Asked by At

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?

2

There are 2 answers

0
Suplanus On

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>
4
NycWes On

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.