How to upgrade msbuild to C# 6?

2019-01-10 12:09发布

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

var user = new SingleUserModel(); //all model fields are null
var test = user.User?.Avatar?["blah"];

But when I push my project to the repo and CI starts to build it, build fails because of unsupported ?.

I've installed VS2015 on CI server too but looke like it doesn't use it. What can I do?

CI - CruiseControl .NET Builds with C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe

3条回答
你好瞎i
2楼-- · 2019-01-10 12:28

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
查看更多
Melony?
3楼-- · 2019-01-10 12:33

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.

查看更多
▲ chillily
4楼-- · 2019-01-10 12:44

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
查看更多
登录 后发表回答