none
MSBuild crashes with a "Message" task could not be instantiated exception RRS feed

  • Question

  • I'm attempting to convert a .net 2.0 app to .net 3.5.  Part of the application builds a new project file and then compiles the project to produce a dll.  Here's the code that invokes msbuild

     

    Code Block

    // We need to tell MSBuild where msbuild.exe is, so it can launch child nodes

    string parameters = @"MSBUILDLOCATION=" + System.Environment.GetFolderPath(System.Environment.SpecialFolder.System) + @"\..\Microsoft.NET\Framework\v3.5";

    // We need to tell MSBuild whether nodes should hang around for 60 seconds after the build is done in case they are needed again

    bool nodeReuse = true; // e.g.

    if (!nodeReuse)

    {

    parameters += ";NODEREUSE=false";

    }

    // We need to tell MSBuild the maximum number of nodes to use. It is usually fastest to pick about the same number as you have CPU cores

    int maxNodeCount = 3; // e.g.

    // Create the engine with this information

    Engine buildEngine = new Engine(null, ToolsetDefinitionLocations.Registry , maxNodeCount, parameters);

    bool goodBuild = buildEngine.BuildProjectFile(projectFileName);

    // Finish cleanly

    buildEngine.Shutdown();

     

     

     

     

    when I call BuildEngine.BuildProjectFileName(), it generates the following error:

     

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(493,9): error MSB4127: The "Message" task could not be instantiated from the assembly "Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". 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.Message' totype 'Microsoft.Build.Framework.ITask'.C:\Windows\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(493,9): error MSB4060: The "Message" task has been declared or used incorrectly, or failC:\Windows\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(493,9): error MSB4127: The "Message" task could not be instantiated from the assembly "Microsoft.Build.Tasks, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". 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.Message' totype 'Microsoft.Build.Framework.ITask'.C:\Windows\Microsoft.NET\Framework\v2.0.50727\Microsoft.Common.targets(493,9): error MSB4060: The "Message" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.ed during construction. Check the spelling of the task name and the assembly name.

     

    I have a reference to the 3.5 assemblies for Microsoft.Build.Engine and Microsoft.Build.Framework in this project.

     

    Can someone shed some light on the source of these errors and suggest a remediation.

     

    Thanks

    Thursday, December 13, 2007 2:37 AM

Answers


  • I believe your host.exe will need an assembly redirect in the exe.config file.

    <?xml version ="1.0"?>
    <configuration>
            <runtime>
            <assemblyBinding xmlns="urnchemas-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="3.5.0.0"/>
                </dependentAssembly>
                    <assemblyIdentity name="Microsoft.Build.Engine" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
                    <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/>
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
    </configuration>


    The reason is msbuild 3.5 uses the Microsoft.Build.Framework 3.5.0.0 when passing data to tasks while the 2005 version of the tasks assembly uses Microsoft.Build.Framework 2.0.0.0 types.  When the 2.0 tasks pass data to the 3.5 engine they use 2.0 framework types and the clr cannot cast these to the same types defined in the 3.5 framework assembly.


    We can however use an assembly redirect to tell the clr to redirect calls made to 2.0 version of the framework assembly to use the 3.5 framework assembly.

    Friday, December 14, 2007 6:03 AM
    Moderator

All replies


  • I believe your host.exe will need an assembly redirect in the exe.config file.

    <?xml version ="1.0"?>
    <configuration>
            <runtime>
            <assemblyBinding xmlns="urnchemas-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="3.5.0.0"/>
                </dependentAssembly>
                    <assemblyIdentity name="Microsoft.Build.Engine" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
                    <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="3.5.0.0"/>
                </dependentAssembly>
            </assemblyBinding>
        </runtime>
    </configuration>


    The reason is msbuild 3.5 uses the Microsoft.Build.Framework 3.5.0.0 when passing data to tasks while the 2005 version of the tasks assembly uses Microsoft.Build.Framework 2.0.0.0 types.  When the 2.0 tasks pass data to the 3.5 engine they use 2.0 framework types and the clr cannot cast these to the same types defined in the 3.5 framework assembly.


    We can however use an assembly redirect to tell the clr to redirect calls made to 2.0 version of the framework assembly to use the 3.5 framework assembly.

    Friday, December 14, 2007 6:03 AM
    Moderator
  • A litte bit more diligence the next time please:

    <?xml version="1.0" ?> 
    <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="3.5.0.0"/>  
          </dependentAssembly> 
          <dependentAssembly> 
            <assemblyIdentity name="Microsoft.CompactFramework.Build.Tasks" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>  
            <bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="9.0.0.0"/>  
          </dependentAssembly> 
        </assemblyBinding> 
      </runtime> 
    </configuration> 


    that colon took me three hours.
    Friday, October 24, 2008 6:15 PM
  • Awesome!  Thanks so much.  Worked like a charm trying to get an XNA game project to build assets on demand.
    Monday, May 10, 2010 5:02 AM
  • Thanks a lot. I have been trying to build .msbuild file from C# program and i got success in that.

    Hardik
    Monday, June 28, 2010 1:01 PM
  • Ensure you have the following Nuget packages installed:

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

    (see https://github.com/dotnet/roslyn/issues/17401#issuecomment-282605950)

    Friday, December 8, 2017 9:44 PM