0

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:

enter image description here

Why is this happening and how can I fix it?

Mo Sadeghipour
  • 489
  • 8
  • 25

3 Answers3

0

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>
FrqSalah
  • 472
  • 3
  • 9
0

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.

kpull1
  • 1,623
  • 15
  • 19
  • That constructor overload needs other parameters `globalProperties` and `hostServices` what should I set them as? – Mo Sadeghipour Feb 27 '18 at 08:19
  • Thanks, I tested your solution but it didn't solve my problem. I've changed the screenshot by the way. Could you please take a look at it? – Mo Sadeghipour Feb 27 '18 at 08:22
  • It looks like you have post-build events. Is it possible? I haven't found yet how to deal with them... If I found the answer in next weeks, I'll update this answer. – kpull1 Mar 06 '18 at 07:27
  • I don't think so, I tried many different things but none worked out for me – Mo Sadeghipour Mar 08 '18 at 09:20
0

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.

Mo Sadeghipour
  • 489
  • 8
  • 25