BuildManager to use another version of MSBuild

让人想犯罪 __ 提交于 2019-11-26 16:45:23

问题


The following code tries to build a Solution programmatically, using BuildManager:

ProjectCollection pc = new ProjectCollection();
pc.DefaultToolsVersion = "12.0";
pc.Loggers.Add(fileLogger);
Dictionary<string, string> globalProperty = new Dictionary<string, string>();
BuildRequestData buildRequest = new BuildRequestData(solutionName, globalProperty, null, new[] { "Build" }, null);

BuildParameters buildParameters = new BuildParameters(pc)
{
    DefaultToolsVersion = "12.0",
    OnlyLogCriticalEvents = false,
    DetailedSummary = true,
    Loggers = new List<Microsoft.Build.Framework.ILogger> { fileLogger }.AsEnumerable()
};

var result = BuildManager.DefaultBuildManager.Build(buildParameters, buildRequest);

When I run this code (it doesn't build anything btw), I can see that the following compiler csc.exe is being used, in addition to one particular version of winmdexp.exe:

C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Csc.exe
ExportWindowsMDFile:
    C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools\winmdexp.exe 

But when I successfully build a solution using VS IDE, the following information comes up:

C:\Program Files (x86)\MSBuild\12.0\bin\Csc.exe
ExportWindowsMDFile:
    C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\winmdexp.exe

Why is this happening in my code & how can I change it?


回答1:


As of Visual Studio 2013, MSBuild is no longer a .NET Framework component. This meant some restructuring of packaging and deployment. The old MSBuild framework still lives in the .NET Framework folders. However when you install Visual Studio 2013, the Microsoft Build Tools 12.0 are also installed to C:\Program Files (x86)\MSBuild\12.0. Note that the Build Tools are available separately from Visual Studio here.

I encountered the same situation as you as well when I first tried this. The issue is that you probably have referenced the old "4.0" MSBuild Framework assemblies. You need to reference the new 12.0 assemblies located in C:\Program Files (x86)\MSBuild\12.0\bin (you'll have to browse there in VS to add the references). You probably need Microsoft.Build, Microsoft.Build.Engine, and Microsoft.Build.Framework. Once I updated those references I saw it was using the same tools as VS 2013 when building.



来源:https://stackoverflow.com/questions/25225025/buildmanager-to-use-another-version-of-msbuild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!