2

I am trying to create a custom MSBuild script in C#, using the newer Microsoft.Build.Evaluation API. The problem I have is that this newer API does not support .sln files. The older deprecated Microsoft.Build.Engine API does support .sln files, but I'd like to use the newer one because 1) it's not deprecated and 2) there seems to be more online documentation and usage to reference. I've seen that MSBuild can create a .metaproj file when is successfully compiles a solution, when this assignment is made in CMD: set MSBuildEmitSolution=1. I need the .metaproj file to be able to compile the solution in the first place. Is there anything in the API for converting .sln to .metaproj? Is there any library out there for parsing .sln files?

JamesFaix
  • 8,050
  • 9
  • 37
  • 73
  • Do you mean the `Microsoft.Build.BuildEngine` API does support .sln files? If not, you can use API `Microsoft.Build.BuildEngine` to programmatically build sln file: https://stackoverflow.com/questions/6511380/how-do-i-build-a-solution-programmatically-in-c. Besides, The .NET 4.0 version of the Microsoft.Build assembly contains a SolutionParser class in the Microsoft.Build.Construction namespace that parses Visual Studio solution files. https://stackoverflow.com/questions/707107/parsing-visual-studio-solution-files. – Leo Liu Oct 18 '17 at 02:36

1 Answers1

3

I figured it out after more searching. Finding good examples online is a little difficult because of the two different versions of the MSBuild API, and the popularity of just running MSBuild from the command line.

Here is the code that is now working for me, using the newer MSBuild API:

var pc = new ProjectCollection();

var parameters = new BuildParameters(pc)
{
    Loggers = new[] { _logger } //Instance of ILogger instantiated earlier
};

var request = new BuildRequestData(
    projectFullPath: pathToMySlnFile, //Solution file path
    globalProperties: myPropertyDictionary,
    toolsVersion: null,
    targetsToBuild: myTargetsArray,
    hostServices: null,
    flags: BuildRequestDataFlags.ProvideProjectStateAfterBuild);

var buildResult = BuildManager.DefaultBuildManager.Build(parameters, request);
JamesFaix
  • 8,050
  • 9
  • 37
  • 73
  • thanks for sharing your solution here, you could mark it as the answer, so it could help other community members who get the same issues. – Leo Liu Oct 23 '17 at 01:20
  • @Leo-MSFT Actually, I am now seeing some very strange behavior, and I was seeing part of this before which is why I confirmed your solution and then unconfirmed it once. If I run in the debugger with this path added as a property, it works, but it will fail to find the correct targets file when not in the debugger. If I remove the path property the opposite is true. When it fails with the path added outside of the debugger, it is looking in the specified directory for v14. The Microsoft.Build packages do not have a v15 yet. Maybe this is part of it – JamesFaix Nov 07 '17 at 23:26
  • This didn't work for me. This did the trick: https://stackoverflow.com/a/14349500/3286975 (NOTE: the BuildEngine API is deprecated). – z3nth10n Feb 20 '19 at 04:01