1
votes

I want to start compiling a VS2017 solution using Miscrosoft.Build assemblies from another code.

Here is my simplified code:

namespace Test
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            RebuildSolution(@"mySolution.sln", "Release", "x86", "4.6.1");
        }

        /// <summary>
        /// Rebuild visual studio solution.
        /// </summary>
        /// <param name="slnPath">Path to .sln file</param>
        /// <param name="configuration">Configuration to use (Release, Debug, ...)</param>
        /// <param name="platform">Target platform (x86, x64, AnyCPU, ...)</param>
        /// <param name="toolsversion">Tools version to use (4.0, 12.0, ...)</param>
        public static void RebuildSolution(string slnPath, string configuration, string platform, string toolsversion)
        {
            if (!File.Exists(slnPath))
            {
                throw new Exception("sln file does not exist : " + slnPath);
            }

            ConsoleLogger cl = new ConsoleLogger(LoggerVerbosity.Minimal);

            Dictionary<string, string> globalProperty = new Dictionary<string, string>
            {
                { "Configuration", "Release" },
                { "Platform", "x86" },
                { "RebuildT4Templates" , "false" },
                { "ToolsVersion", "15.1" },
                { "VisualStudioVersion", "15.1" }
            };

            BuildRequestData buildRequest = new BuildRequestData(slnPath, globalProperty, null, new string[] { "Rebuild" }, null);

            BuildParameters bp = new BuildParameters();
            bp.Loggers = new List<ILogger> { cl }.AsEnumerable();

            BuildResult buildResult = BuildManager.DefaultBuildManager.Build(bp, buildRequest);
        }
    }
}

After having added the latest versions (15.1.0.0) of Miscrosoft.Build.* assemblies to my solution, I have this error message that I can not fix (during execution) :

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets(52,5): error MSB4127: Impossible d'instancier la tâche "Csc" à partir de l'assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\Microsoft.Build.Tasks.CodeAnalysis.dll". Vérifiez que l'assembly de tâche a été généré à l'aide de la même version de l'assembly Microsoft.Build.Framework que celle installée sur votre ordinateur et qu'il ne manque pas de redirection de liaison pour Microsoft.Build.Framework dans votre application hôte. Impossible d'effectuer un cast d'un objet de type 'Microsoft.CodeAnalysis.BuildTasks.Csc' en type 'Microsoft.Build.Framework.ITask'. C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Roslyn\Microsoft.CSharp.Core.targets(52,5): error MSB4060: La tâche "Csc" a été déclarée ou utilisée de manière incorrecte, ou a échoué penda nt la construction. Vérifiez l'orthographe du nom de la tâche et de l'assembly.

(sorry for french)

Can your help me ?

1
It results from a change after vs2017, after no longer placing the MSBuild in GAC, the way which is suggested in Microsoft document is using Microsoft.Build.Locator.LoLance

1 Answers

0
votes

It has something to do with the Toolsets can't be found.

Something similar to this issue. It seems that this issue occurs after vs2017(15.x).

I followed your steps and meet the same issue like below: enter image description here

And I found useful info at this document:

Visual Studio 2017 and later versions no longer place MSBuild in the GAC or modifies the registry. Unfortunately, this means that applications that wish to use the MSBuild API to evaluate or build projects can't implicitly rely on the Visual Studio installation.

Here is the simple workaround described in the doc which I test it works:

  1. In vs, Go to solution explorer=>references, change the Copy Local properties of Microsoft.Build.*(Change all Microsoft.Build and Microsoft.Build.XXX) from true to false.

  2. Add a NuGet package reference to Microsoft.Build.Locator.

  3. Add a call to the Locator API before calling any method that uses MSBuild. MSBuildLocator.RegisterDefaults(); // call to Locator API before calling your RebuildSolution(...)

  4. Build the project and make sure it doesn't contain any Microsoft.Build.*.dll assemblies other than Microsoft.Build.Locator.dll.

  5. Also,if you want to build the project with X86, you must define the X86 property(Which can be found in xx.csproj). So I suggest you can use { "Platform", "Any CPU" } if you haven't define the configuration in the project you build.