How to upgrade msbuild to C# 6?

前端 未结 3 1144
失恋的感觉
失恋的感觉 2020-12-02 12:56

I want to use C# 6 in my project (null propagation, other features).

I\'ve installed VS 2015 on my PC and it works brilliantly and builds test code like



        
相关标签:
3条回答
  • 2020-12-02 13:14

    You probably already have this working, but this might help someone else in the future. I came across this question recently and it got me moving in the right direction and ultimately led to a solution.

    Another possible solution to this is manually updating your project files to target the MSBuild version you want your projects to be built with.

    I've recently gone through a TeamCity build server update and I've already installed the Microsoft Build Tools 2015 on it. I thought I had everything in place on the build server, I had my solution targeting C# 6.0, and I had every project targeting .net 4.6.1. Like you, everything with C# 6.0-specific code built just fine in my local environment, but my TeamCity build server didn't like any of it.

    As mentioned by others, I tried using the Microsoft.Net.Compilers NuGet package. The latest version of it allowed the build to work on my build server, but it wouldn't let me publish my code locally (a requirement of mine). Earlier versions of that NuGet package would let me publish, but the build wouldn't work.

    What I found that I had to do was ultimately modify each project file in my solution to specifically target the MSBuild version that could handle C# 6.0 code. In each of my project files, I found a line similar to the following line:

    <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    

    with the key component of that line being the ToolsVersion portion of it. I simply changed this line on my project files to read the following:

    <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    

    The difference here was that I was targeting version 14, not 4. Version 14.0 corresponds with Build Tools 2015. By changing this, my TeamCity build server used the correct MSBuild version and was able to build my C# 6.0 code.

    I also had to manually update the TargetFrameworkVersion xml node of this to use 4.6.1 because VS2015 wasn't doing something right and messed up my local build, but that's not relevant here.

    Please, someone correct me if I'm wrong, but just for reference, I think the version numbers go something like this:

    4.0 = VS2012

    12.0 = VS2013

    14.0 = VS2015

    15.0 = VS2017

    I believe if you wanted to use .net 4.7, you'd have to have the Build Tools 2017 installed and have your projects targeting 15.0 instead of 14.0, but I haven't verified this.

    0 讨论(0)
  • 2020-12-02 13:31

    Make sure you call:

    C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe
    

    That's the version of MsBuild that ships with Visual Studio 2015 and calls the C# compiler that understands this. You can get this version of MsBuild on your system by installing any edition of Visual Studio 2015 or by installing the stand-alone Microsoft Build Tools 2015.

    Adding a reference to the following NuGet package will also force use of the new compiler:

    Install-Package Microsoft.Net.Compilers
    

    Please note Install-Package will pick the latest available version which may not be the one you are looking for. Before you install, please check the release notes and dependencies to resolve the underlying issue with the version being dealt with, which in this case, was more specific to VS 2015.

    So for Visual Studio 2015:

    Install-Package Microsoft.Net.Compilers -Version 1.0.0
    
    0 讨论(0)
  • 2020-12-02 13:36

    You can by the way also install the "Microsoft Build Tools 2015" instead of VS2015 on your build server.

    https://www.microsoft.com/en-us/download/details.aspx?id=48159

    It installs MSBuild to the same path:

    C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe
    
    0 讨论(0)
提交回复
热议问题