Nmktronas Nmktronas - 10 months ago 65
C# Question

msbuild.exe works but msbuild API does not

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">

The error I get:

The "EntityDeploySplit" task could not be instantiated from the
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

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

Answer Source

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