msbuild - 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:
- Updating the NuGetPackages for Microsoft.Build.
- Installing the NuGet Tools from the VS-installer
- Registered the build dll's in the gac as explained in this link (Though I fear that this was unwise)
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.
解决方案
我应用了两项使其工作的更改:
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(我很可能不应该这样做)。
推荐阅读
- node.js - 如何告诉 Jest 空间实际上是空间?
- cmake - CMake 安装出现问题,正在中止构建。CMake 可执行文件是 cmake
- php - 如何短语/分解 XML 数据?
- flutter - 如何向 Material App Flutter 添加多个主题参数?
- windows - 从 WMIC 查询中排除一些输出
- python - How to "upgrade" hourglass pattern generator?
- flutter - 如何在午夜从 Flutter 更新 Cloud Firestore 中的值
- java - 如何在 Java 代码中解析和编辑 MS Visio 文件
- rust - Rust Playground 和本地机器中 reqwest 示例的错误
- unity3d - 在着色器中实现权重以生成程序地形