2
votes

I want to automate my builds using MSBuild API, but it gives me an error. I have my automation project in Visual Studio 2015. It references MSBuild v12.0 dlls.

Code I'm using to build a solution:

private bool Execute()
{
    var buildProperties = new Dictionary<string, string>();
    buildProperties["Configuration"] = "Debug";
    var solution = @"C:\test\test.sln";
    bool success = Build(solution, buildProperties)
    return success;
}

private bool Build(string solution, Dictionary<string, string> buildProperties)
{
    Guard.ValidPath(solution, nameof(solution));
    BuildParameters buildParam = new BuildParameters()

    BuildRequestData buildRequest = new BuildRequestData(solution, buildProperties, null, new string[] { "Build" }, null);
    BuildResult buildResult = BuildManager.DefaultBuildManager.Build(buildParam, buildRequest);
    if (buildResult.OverallResult == BuildResultCode.Success)
        return true;

    return false;
}

The solution has one project. It's configuration:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
etc.

The error I get:

: ERROR C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Data.Entity.targets(65,5): The "EntityDeploySplit" task could not be instantiated from the assembly "C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Data.Entity.Build.Tasks.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.Data.Entity.Build.Tasks.EntityDeploySplit' to type 'Microsoft.Build.Framework.ITask'

Any idea how to overcome this error? Why this happens?

2
Your question is answered in this related post.Axel Kemper

2 Answers

5
votes

Had a similar problem but another error message. My web projects couldn't build because they tried to cast their tasks to Microsoft.Build.Framework.ITask. I think the problem was that they used an older version and therefore unable to cast to the new version of ITask. Found this solution for MSBuild 3.5 and it works for 14.0 too.

MSDN Forum: MSBuild crashes with a "Message" task could not be instantiated exception

I changed 3.5 to 14.0 and added this to my app.config:

<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="14.0.0.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
2
votes

I managed to solve this by referencing Microsoft.Build.Framework v14.0, other Microsoft.Build dlls are v12.0. Now it works!