首页 > 解决方案 > Version conflicts when trying to use MSBuild from a Console project

问题描述

Ok, I am at my wits' end. We have a VB.Net application with many dependent assemblies (all contained in the solution), that builds well from with the Visual Studio IDE. Now I want to automate the build to produce three different versions (and copy the results to our installer package). After some research I came up with the following code, which I use from a C# console project:

var projectFileName = @"D:\NetCode\60-40\Applications\ControlViewer\BridgeIt.vbproj";
string target = "Build";
var properties = new Dictionary<string, string>();
properties.Add("Configuration","NBBR");
properties.Add("Platform", "AnyCPU");
var buildRequest = new BuildRequestData(projectFileName, properties, null, new[] { target }, null);
var buildParams= new BuildParameters(new ProjectCollection());
var logger = new BasicLogger();
buildParams.Loggers = new List<ILogger>() { logger};
BuildManager.DefaultBuildManager.ResetCaches();
var result = BuildManager.DefaultBuildManager.Build(buildParams, buildRequest);
Debug.Print(logger.GetLogString());

This builds all assemblies, only at the very last I get the following error:`

ERROR C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(1657,5): The "GetReferenceNearestTargetFrameworkTask" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.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. Kan een object van het type NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask niet converteren naar het type Microsoft.Build.Framework.ITask. : The final remark is in Dutch and describes an `InvalidCastException'.

I tried:

Well, nothing works. Could it be a C#/VB.Net compatibility issue?

Other thoughts: Most of the projects in the solution have come from VS 2015. Currently we are using VS 2017.

标签: msbuild

解决方案


我应用了两项使其工作的更改:

var buildParams = new BuildParameters(new ProjectCollection() { DefaultToolsVersion = "15.0" });

我将它添加到 app.config 中:

<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-15.1.0.0" newVersion="15.1.0.0" />
  </dependentAssembly>
</assemblyBinding>
</runtime>

并且不要忘记我在 GAC 中安装了 NuGet Build dll(我很可能不应该这样做)。


推荐阅读