【问题标题】:c# error CS1056: Unexpected character '$' with Jenkins or MSbuildc# 错误 CS1056: Jenkins 或 MSbuild 出现意外字符 '$'
【发布时间】:2017-03-28 09:08:11
【问题描述】:

我需要使用 Jenkins 继续集成,在我的代码中我编写了类似

的代码
throw new ArgumentNullException($"The {nameof(Id)} cannot be null");

如果我构建应用程序,它会成功构建,但如果我使用 MsBuild(or) Jenkins 构建相同的应用程序,

我收到类似

的错误
Web\WebSharedHelper.cs "C:\WINDOWS\TEMP\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
Security\ApiUserToken.cs(46,32): error CS1056: Unexpected character '$' [C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj]
Security\ApiUserToken.cs(58,32): error CS1056: Unexpected character '$' [C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj]
Security\ApiUserToken.cs(62,26): error CS1056: Unexpected character '$' [C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj]
Done Building Project "C:\Program Files (x86)\Jenkins\workspace\OssiaCICD\common\trunk\Cota.Common.Core\Cota.Common.Core.csproj" (default targets) -- FAILED.

我正在为 MSBuild 使用 v4.0.30319 FrameWork。

【问题讨论】:

  • 编译成功,编译成功,编译报错?您能否指定这两个版本之间的区别?我猜用于第二次构建的编译器不知道 C#6(字符串插值)?
  • 请注意,要使用字符串插值,您必须使用 C#6 及更高版本
  • 我正在从它成功构建的视觉工作室构建。但是如果我使用 MSBuild 从命令提示符构建我会收到错误消息。我只知道版本的问题,但为什么以及如何解决该问题。
  • 我已经浏览了这个链接,但这是完全不同的问题,因此我要求保持原样。

标签: c# asp.net .net jenkins


【解决方案1】:

我通过在 jenkins 上使用 C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe 而不是 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe 解决了这个问题。

【讨论】:

  • 使用 @serkanh 提到的 Visual Studio 构建工具中的 MSBuild.exe 对我来说是解决方法,但我使用的是 15.0 版(VS2017)。它还更正了与 ReportViewer 控件和版本号相关的警告。 (构建是通过 Jenkins 完成的。)
【解决方案2】:

$ 字符串插值符号仅在 C# 6+ 中可用。确保您使用 C#6 进行编译。 MSBuild 14.0+ 支持 C#6。

【讨论】:

    【解决方案3】:

    如果您的 Visual Studio 版本早于 2015 年,则默认情况下 IDE 不提供您需要编译它的 C#6。

    使用 Visual Studio 2015 或更高版本。它应该可以解决问题。

    【讨论】:

    • 感谢您的快速回复,但我在仅使用 msbuild 的 Visual Studio 问题时没有收到任何错误
    • 所以可能是msbuild版本太旧了:See this post
    【解决方案4】:

    可以通过多种方式解决此问题。请在这里查看我的答案 对于第一个选项:

    第一个https://stackoverflow.com/a/42931980/819153

    第二个选项 - 尝试在运行持续集成的 tfs 上安装 Visual Studio,这样可以省去很多麻烦

    【讨论】:

      【解决方案5】:

      正如一位绅士所建议的那样,我使用 C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe 而不是 C:\Windows\Microsoft.NET\Framework64\v4.0.30319\MSBuild.exe通过构建代理。 它帮助了我。

      我在构建代理中有 MSBuild 版本 14.0.25420.1。

      【讨论】:

        【解决方案6】:

        在我的情况下,在下面工作..

        删除了 langversion 节点并添加了对 csproj 的 netstandard 引用,以便 msbuild 可以与 Jenkins 或 msbuild 命令一起使用。

        在 .csproj 文件的下面几行注释。

        <!--    <LangVersion>8.0</LangVersion>-->
        

        并为more-info添加&lt;Reference Include="netstandard" /&gt;

        【讨论】:

          猜你喜欢
          • 2010-09-17
          • 1970-01-01
          • 2021-04-21
          • 2017-08-13
          • 2019-02-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-19
          相关资源
          最近更新 更多