错误 CS1056: 意外字符 '$' 运行 tfs 持续集成过程中的 msbuild

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

我有一个框架针对的项目 .NET Framework 4.6.1,作为 tfs 持续集成过程的一部分,我们创建了一个构建解决方案任务以确保代码正确编译。
现在 TFS 服务器有最新版本的 .Net Famework 4.6.2。在寄存器中,这是框架的 Release 键的值

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

但是当构建运行时出现了这个错误:

Error CS1056: Unexpected character '$'

我不想用 string.Format 替换字符串插值来解决这个问题,请提供另一种解决方法来解决它。

我需要在 TFS 服务器上安装其他东西吗?

我只是猜测您有带字符串插值的代码,但没有 proper build tools.

使用新值 (14.0) 安装 MS Build tools 2015 into %ProgramFiles%\MSBuild.0\bin you need to override the MSBuild version for build server 后。

您应该阅读 MSDN 文章(或 ),但是 TL;DR 您的选择是:

  • 当您从命令行构建项目或解决方案时,使用 /ToolsVersion 开关(或简称 /tv)覆盖版本:

    msbuild.exe someproj.proj /tv:14.0 /p:Configuration=Debug
    
  • 通过在 MSBuild 任务上设置 ToolsVersion 参数来覆盖版本:

    <MSBuild Projects="myProject.proj"  
        ToolsVersion="14.0"  
        Targets="go" />
    
  • 通过在解决方案中的项目上设置 $(Project.ToolsVersion) 属性 来覆盖版本。这使您可以在 ToolsetVersion 不同于其他项目的解决方案中构建项目:

    <Project ToolsVersion="14.0" ... </Project>  
    

The order of precedence, from highest to lowest, used to determine the ToolsVersion is:

  1. The ToolsVersion attribute on the MSBuild task used to build the project, if any.
  2. The /toolsversion (or /tv) switch that's used in the msbuild.exe command, if any.
  3. If the environment variable MSBUILDTREATALLTOOLSVERSIONSASCURRENT is set, then use the current ToolsVersion.
  4. If the environment variable MSBUILDTREATHIGHERTOOLSVERSIONASCURRENT is set and the ToolsVersion defined in the project file is greater than the current ToolsVersion, use the current ToolsVersion.
  5. If the environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION is set, or if ToolsVersion is not set, then the following steps are used:
    • The ToolsVersion attribute of the Project element of the project file. If this attribute doesn’t exist, it is assumed to be the current version.
    • The default tools version in the MSBuild.exe.config file.
    • The default tools version in the registry. For more information, see Standard and Custom Toolset Configurations.
  6. If the environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION is not set, then the following steps are used:
    • If the environment variable MSBUILDDEFAULTTOOLSVERSION is set to a ToolsVersion that exists, use it.
    • If DefaultOverrideToolsVersion is set in MSBuild.exe.config, use it.
    • If DefaultOverrideToolsVersion is set in the registry, use it.
    • Otherwise, use the current ToolsVersion.

问题可以通过安装 Nuget 包来解决 Microsoft.Net.Compilers。以下是我突出显示的答案的 link:

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.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.

根据我的经验,我用 3 种不同的方式解决了这个问题:

1- 刚从 Nuget 获取包

2- 在 tfs 服务器上安装 Microsoft Build Tools 2015

3- 大锤和最后一个选项但对我来说是最好的,因为你不需要处理对 nuget 的依赖,正在 [=27= 所在的 tfs 服务器上安装 visual studio 版本] 过程。

希望对您有所帮助

您可能正在构建错误 MSbuild.exe;在 Visual Studio (它工作的地方)进行编译并检查输出中的日志。应该是这样的:

1>Target "GetReferenceAssemblyPaths" in file "C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild.0\Bin\Microsoft.Common.CurrentVersion.targets" 

确保你在那个 Bin 目录中使用 MSBuild.exe,在我的例子中;

C:\Program Files (x86)\Microsoft Visual Studio17\Community\MSBuild.0\Bin\MSbuild.exe

我的解决方案分为两部分:

1) 使用 Visual Studio 2015,select 您的 Web 项目,单击 项目 菜单,select "Enable C# 6"

2) 我需要将以下内容添加到我的生产结束 web.config,就在配置结束标记之前。请注意,版本号将来可能会发生变化,但关键是在步骤 1 中启用后 web.config 在您的开发中寻找类似的文本,并确保将其转移到生产环境中。

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>

Microsoft.Net.Compilers 没有用,但是从 Nuget 安装 DotNetCompilerPlatform 可以。

您必须使用 msbuild 版本 15。

我遇到这个问题是因为我使用了错误的 msbuild 路径。我使用的 C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe 对应于旧版本的 Visual Studio 和框架,现在我使用 Visual Studio 2019,所以正确的构建路径是 C:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin\MSBuild.exeC:\Program Files (x86)\Microsoft Visual Studio19\Community\MSBuild\Current\Bin\amd64\MSBuild.exe取决于您要构建的架构。