简体   繁体   中英

Building Project Programmatically Fails Using Microsoft.Build

I installed the three following packages into my console application:

Microsoft.Build
Microsoft.Build.Framework
Microsoft.Build.Tasks.Core
Microsoft.Build.Utilities.Core

And I tried to use the following method to build a project:

        static void Build(string projectPath)
        {
            var logger = new ConsoleLogger(LoggerVerbosity.Normal);
            logger.ShowSummary = true;
            var manager = BuildManager.DefaultBuildManager;

            var projectInstance = new ProjectInstance(projectPath);
            var result = manager.Build(
                new BuildParameters()
                {
                    DetailedSummary = true,
                    Loggers = new List<ILogger>() { logger }
                },
                new BuildRequestData(projectInstance, new string[] { "Build" }));
            var buildResult = result.ResultsByTarget["Build"];
            var buildResultItems = buildResult.Items;
        }

However, after I ran the code, I got the error that described in the following image:

在此处输入图片说明

Why is this happening and how can I fix it?

I think you're not using tht right MSBuild version. Try to set the variable explicitly in your .proj :

<MSBuildExtensionsPath>C:\Program Files (x86)\MSBuild</MSBuildExtensionsPath>

One of BuildRequestData constructor overloads supports a parameter called "toolVersion". Since you are using Visual Studio 2017, set it as "15.0".

EDIT: I quitted using the .Net Framework provided MSBuild version (the one located here): System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()

C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319

Instead, I'm using the one located here:

C:\\Program Files (x86)\\MSBuild\\{version}\\Bin

This version provide extra parameters as LangVersion or DeployOnBuild .

It seems the best solution is to use MSBuild command line in Process class. A working sample is as follows:

            var buildOutput = new List<string>();
            var buildError = new List<string>();
            var buildProcess = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\MSBuild.exe",
                    Arguments = projectPath + " /t:Rebuild /p:Configuration=Debug",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                }
            };

            buildProcess.Start();
            while (!buildProcess.StandardOutput.EndOfStream)
            {
                buildOutput.Add(buildProcess.StandardOutput.ReadLine());
            }
            while (!buildProcess.StandardError.EndOfStream)
            {
                buildError.Add(buildProcess.StandardError.ReadLine());
            }

And then you could use the output to determine whether the build was successful or not. The important note is that you have to find the correct path of MSBuild.exe file as there are several versions of this file and in my case (VS 2017) the correct path is the one in the sample code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM