0

I recently start to work on a legacy ASP.NET codebase based on .NET framework 4.0. We managed to pass everything from Visual Studio 2012 to VS 2017, updated the build server with a new version of Jenkins and installing .NET framework 4.7.x.

Locally we can write C# code of the newest version (7.3) and the build works (VS doesn't use MSBuild if I remember right), but when we deploy on the build server the build fails because there MSBuild cannot recognize constructs newer than C# 4.0. To avoid mistakes I fixed the lang version to 4.0 (advanced build properties on projects), so if I write too new C# VS blocks me in dev, but we would like to start using new C#.

We also tried to fix C# 7.3 directly in the project (<LangVersion>7.3</LangVersion> in PropertyGroup inside csproj) and the but ToolsVersion property of Project element (csproj) to 14.0, but then building we MSBuild fails with the error:

CS1617: Invalid option ‘6’ for /langversion; must be ISO-1, ISO-2, 3, 4, 5 or Default

Here it's explained that what I want to do it is possible: https://www.dotnetcurry.com/dotnet/1427/build-apps-different-dotnet-framework-versions

No matter which .NET framework version we target in the project, the C# language version in use will not change. That’s fine because the vast majority of language features that were introduced in later versions of the language don’t depend on the CLR or specific APIs. They are only syntactic sugar and the bytecode generated by the compiler will still work in .NET framework 2.0.

Anyone have an idea of what mistake are we doing?

Mauro Piccotti
  • 1,807
  • 2
  • 23
  • 34
  • https://stackoverflow.com/a/32534628/11683? – GSerg Nov 01 '18 at 10:40
  • as far as I know there where no breaking changes in each of C# lang version (correct me if I wrong), so code writen in C# 4.0 shoul behave exactly the same in C# 7.3 – vasily.sib Nov 01 '18 at 10:43
  • @GSerg I read that one looking for the error, but that I got that error doing random experiments and I'm not sure if it's my case. Probably I should read better all the answers. @vasily.sib my problem is that MSBuild apparently cannot understand anything new than C# 4.0, while we would like to code using C# 7.x (if I write a `nameof`, for instance, the build fails) – Mauro Piccotti Nov 01 '18 at 10:52
  • @MauroPiccotti MSBuild is a part of .net, also VS uses MSBuild too, the only exception is that it uses its own (hosted) version of MSBuild as stated [here](https://learn.microsoft.com/ru-ru/visualstudio/msbuild/msbuild?view=vs-2017#use-msbuild-in-visual-studio) Can you try to update your .net framework to the latest available and check if MSBuild will understand C# 7.3? – vasily.sib Nov 01 '18 at 11:08
  • @vasily.sib on the server we installed .net framework 4.7.x, as written in the question. It doesn't work, probably we have some wrong configurations in csproj or on the build server. – Mauro Piccotti Nov 01 '18 at 11:19
  • @vasily.sib There is a ton of new syntax in c# 7 that is not on c# 4 – JSON Mar 01 '21 at 20:23

1 Answers1

0

The problem was that on the build server MSBuild wasn't properly installed and build scripts got an old one. Installing Visual Studio 2017 Build tools and fixing the path on the script we solved.

After we had the problem "The “GetReferenceNearestTargetFrameworkTask” task was not found" we solved like explained here: The "GetReferenceNearestTargetFrameworkTask" task was not found (the right answer depends on what strategy have you used to install VS Buld tools).

Mauro Piccotti
  • 1,807
  • 2
  • 23
  • 34