Error CS1056: Unexpected character '$' running the msbuild on a tfs continuous integration process

Heinrich picture Heinrich · Mar 21, 2017 · Viewed 57.9k times · Source

I have a project that the framework is targeting .NET Framework 4.6.1, as part of the continuous integration process on the tfs we created a Build Solution task to ensure that the code compiles correctly.
Now the TFS server has the latest version of the .Net Famework 4.6.2. On the register this is the value for the Release key of the framework

On all other OS versions: 394806 => .NET Framework 4.6.2

But when the build runs it comes with this error:

Error CS1056: Unexpected character '$'

I don't want to replace the string interpolation with the string.Format to solve this issue, please provide another workaround to solve it.

Do I need to install something else on the TFS server?

Answer

Zinov picture Zinov · Mar 24, 2017

The problem can be fixed installing a Nuget package Microsoft.Net.Compilers. Below is the link of my highlighted answer: Project builds fine with Visual Studio but fails from the command line

That feature is a syntactic sugar for C#6, try to install the latest version of the framework 4.6.2 https://www.microsoft.com/en-us/download/details.aspx?id=53345

Then go to your Project properties and change on the Application option on Target framework to point to the latest. You don't need to change your code to replace the string interpolation with string.Format method to fix it. If you are still getting this error, is because, the compiler that is running your build is not the latest version of C#, try to add the Microsoft.Net.Compilers, from Nuget and compile again, that should resolve the issue. If you want to avoid to install this package, try to open your .csproj and take a look on the ToolsVersion.that should be pointing to the version 12, then change it to 14, but make sure you have installed the latest version of the MSBuild from https://www.microsoft.com/en-us/download/details.aspx?id=48159 or go to C:\Program Files (x86)\MSBuild\14.0\Bin, there you should have this folder with the csc.exe compiler. If even then that doesn't resolve the issue, then try to follow this steps https://msdn.microsoft.com/en-us/library/bb383985.aspx.

In my experience I solved this problem in 3 different ways:

1- just getting the package from Nuget

2- installing Microsoft Build Tools 2015 on the tfs server

3- The sledgehammer and last options but for me the best because you don't need to deal with the dependency on nuget, is installing the visual studio version on the tfs server where you run the process.

Hope this helps