none
How can we use MSBuild in C# to build C# 7.1 code?

    Question

  • Dear community,

    MsBuild does not seem to be able to build code including C# 6 and up features.

    What we tried so far:

    • using MSBuild v14 DLLs under C:\Program Files (x86)\MSBuild\14.0\Bin (Microsoft.Build.dll, Microsoft.Build.Framework.dll) => does not support C# 6 and up
    • using v15 DLLs under C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin and C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin => the both do not work (the exception says the error is in assembly Microsoft.Build.Tasks.Core.dll)
    • command line with MsBuild.exe works like charm but we prefer real assemblies to build the solution programatically and not Process.Start with MsBuild.exe

    the code

    public static bool BuildSolution(string path, string configuration, string platform, ILogger logger) { BuildParameters param = new BuildParameters() { MaxNodeCount = Environment.ProcessorCount, Loggers = new List<ILogger> { logger } }; Dictionary<string, string> globalProperties = new Dictionary<string, string> { { "Configuration", configuration }, // always "Debug" { "Platform", platform }, // always "Any CPU" { "RebuildT4Templates" , "true" }, { "VSToolsPath", @"C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools" }, { "LangVersion", "6" }, { "ToolsVersion", "15.0" }, { "VisualStudioVersion", "15.0" } }; BuildRequestData request = new BuildRequestData(path, globalProperties, null, new[] { "Rebuild" }, null); BuildResult result = BuildManager.DefaultBuildManager.Build(param, request); // throws:

    //Microsoft.Build.Exceptions.InvalidProjectFileException:

    'Die Microsoft.Build.Tasks.Message-Aufgabe konnte nicht aus der Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a-Assembly geladen werden. Die Datei oder Assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden. Stellen Sie sicher, dass die <UsingTask>-Deklaration korrekt ist, die Assembly und alle zugehörigen Abhängigkeiten verfügbar sind und die Aufgabe eine öffentliche Klasse enthält, die Microsoft.Build.Framework.ITask implementiert.  C:\Projects\COMAP\trunk\Client\CoMaPClient.sln.metaproj' return result.OverallResult == BuildResultCode.Success; }

    We also tried excluding parameters such as "VSToolsPath" in every imaginable combination, but it is always the same error (v14: C# 6 not supported and v15 throws that exception above).

    I think the best way to solve this is using v15 MSBuild assemblies and finding out what that exception message means.

    Any help would be appreciated!

    Best regards,

    Daniel


    Tuesday, February 20, 2018 9:23 AM

All replies

  • Hi Daniel Landgraf,

    Welcome to MSDN forum.

    What is the version of your assemblies of Microsoft.Build.Evaluation and Microsoft.Build.Execution when you build the code with MSBuild API? Are you using version 4.0? If yes, please try to use the version 14.0 or 15.0:

    https://www.nuget.org/packages/Microsoft.Build.Framework/

    You can add it by nuget.

    Hope this heklps.


    MSDN Community Support Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, February 22, 2018 8:25 AM
  • Thank you for your answer!

    I tried version 14.0, 15.1 and 15.5 via nuget. Version 15 DLLs actually does know C# 7.1, however it tells me 15.0 is an invalid BuildTools-Version. So I just passed "14.0" as the third parameter at new BuildRequestData() and I get this:

    System.IO.FileNotFoundException: 'Die Datei oder Assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.'
    InnerException: FileNotFoundException: Die Datei oder Assembly "Microsoft.Build.Tasks.Core, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" oder eine Abhängigkeit davon wurde nicht gefunden. Das System kann die angegebene Datei nicht finden.

    both exceptions are obviously in conflict with each other, one says use 15.1 DLL and the other says use 14.0 ?! :D

    So I referenced 14.0 DLL of Microsoft.Build.Tasks.Core, but I got the EXACT same exception including the innerexception.

    One more thing to mention: the 15.5.x nuget dll has version 15.1.0.0 (shown in Reference Properties)...

    Hope someone can help :)

    Thursday, February 22, 2018 9:27 AM
  • IIRC, msbuild.exe.config contains assembly binding redirections. You may need to implement similar ones in your application. Perhaps create a new appdomain and point AppDomainSetup.ConfigurationFile to msbuild.exe.config so that you won't need to update the version numbers if they ever change. Alternatively, https://github.com/Microsoft/MSBuildLocator adds an event handler for AppDomain.CurrentDomain.AssemblyResolve.
    Thursday, February 22, 2018 6:13 PM