简体   繁体   English

错误 CS1056:在 tfs 持续集成过程中运行 msbuild 时出现意外字符“$”

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

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.我有一个项目,该框架针对.NET Framework 4.6.1 ,作为 tfs 上持续集成过程的一部分,我们创建了一个构建解决方案任务,以确保代码正确编译。
Now the TFS server has the latest version of the .Net Famework 4.6.2 .现在 TFS 服务器拥有最新版本的.Net Famework 4.6.2 On the register this is the value for the Release key of the framework在寄存器上,这是框架的 Release 键的值

On all other OS versions: 394806 => .NET Framework 4.6.2在所有其他操作系统版本上: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.我不想用string.Format替换字符串插值来解决这个问题,请提供另一种解决方法来解决它。

Do I need to install something else on the TFS server?我需要在 TFS 服务器上安装其他东西吗?

The problem can be fixed installing a Nuget package Microsoft.Net.Compilers.该问题可以通过安装 Nuget 包 Microsoft.Net.Compilers 来解决。 Below is the link of my highlighted answer: Project builds fine with Visual Studio but fails from the command line下面是我突出显示的答案的链接: Project builds good with Visual Studio but failed 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该功能是 C#6 的语法糖,尝试安装最新版本的框架 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.您无需更改代码即可使用 string.Format 方法替换字符串插值来修复它。 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.如果您仍然收到此错误,是因为运行您的构建的编译器不是最新版本的 C#,请尝试从 Nuget 添加 Microsoft.Net.Compilers 并再次编译,这应该可以解决问题。 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.如果您想避免安装此包,请尝试打开您的 .csproj 并查看 ToolsVersion.that 应该指向版本 12,然后将其更改为 14,但请确保您已安装最新版本的MSBuild 从https://www.microsoft.com/en-us/download/details.aspx?id=48159或转到 C:\\Program Files (x86)\\MSBuild\\14.0\\Bin,你应该有这个文件夹csc.exe 编译器。 If even then that doesn't resolve the issue, then try to follow this steps https://msdn.microsoft.com/en-us/library/bb383985.aspx .如果即使那样也不能解决问题,请尝试按照以下步骤https://msdn.microsoft.com/en-us/library/bb383985.aspx 操作

In my experience I solved this problem in 3 different ways:根据我的经验,我用 3 种不同的方式解决了这个问题:

1- just getting the package from Nuget 1- 刚从 Nuget 获取包

2- installing Microsoft Build Tools 2015 on the tfs server 2- 在 tfs 服务器上安装 Microsoft Build Tools 2015

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. 3- 大锤和最后一个选项,但对我来说是最好的,因为您不需要处理对 nuget 的依赖,是在运行该过程的 tfs 服务器上安装 Visual Studio 版本。

Hope this helps希望这可以帮助

After installing the MS Build tools 2015 into %ProgramFiles%\\MSBuild\\14.0\\bin you need to override the MSBuild version for build server with new value ( 14.0 ).MS Build tools 2015安装到%ProgramFiles%\\MSBuild\\14.0\\bin您需要使用新值 ( 14.0 ) 覆盖构建服务器的 MSBuild 版本

You should read the MSDN article (or this answer ), but TL;DR your options are:您应该阅读 MSDN 文章(或此答案),但TL;DR您的选择是:

  • Override version by using the /ToolsVersion switch (or /tv , for short) when you build the project or solution from the command line: /ToolsVersion构建项目或解决方案时,使用/ToolsVersion开关(或简称/tv )覆盖版本:

     msbuild.exe someproj.proj /tv:14.0 /p:Configuration=Debug
  • Override version by setting the ToolsVersion parameter on the MSBuild task:通过在MSBuild任务上设置ToolsVersion参数来覆盖版本:

     <MSBuild Projects="myProject.proj" ToolsVersion="14.0" Targets="go" />
  • Override version by setting the $(Project.ToolsVersion) property on a project within a solution.通过在解决方案中的项目上设置$(Project.ToolsVersion)属性来覆盖版本。 This lets you build a project in a solution with a ToolsetVersion that differs from that of the other projects:这使您可以在具有不同于其他项目的ToolsetVersion的解决方案中构建项目:

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

The order of precedence, from highest to lowest, used to determine the ToolsVersion is:用于确定ToolsVersion从高到低的优先顺序是:

  1. The ToolsVersion attribute on the MSBuild task used to build the project, if any.用于生成项目的MSBuild任务上的ToolsVersion属性(如果有)。
  2. The /toolsversion (or /tv ) switch that's used in the msbuild.exe command, if any. msbuild.exe命令中使用的/toolsversion (或/tv )开关(如果有)。
  3. If the environment variable MSBUILDTREATALLTOOLSVERSIONSASCURRENT is set, then use the current ToolsVersion .如果设置了环境变量MSBUILDTREATALLTOOLSVERSIONSASCURRENT ,则使用当前的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 .如果设置了环境变量MSBUILDTREATHIGHERTOOLSVERSIONASCURRENT并且项目文件中定义的ToolsVersion大于当前ToolsVersion ,则使用当前ToolsVersion
  5. If the environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION is set, or if ToolsVersion is not set, then the following steps are used:如果设置了环境变量MSBUILDLEGACYDEFAULTTOOLSVERSION ,或者未设置ToolsVersion ,则使用以下步骤:
    • The ToolsVersion attribute of the Project element of the project file.项目文件的Project元素的ToolsVersion属性。 If this attribute doesn't exist, it is assumed to be the current version.如果此属性不存在,则假定为当前版本。
    • The default tools version in the MSBuild.exe.config file. MSBuild.exe.config文件中的默认工具版本。
    • 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:如果未设置环境变量MSBUILDLEGACYDEFAULTTOOLSVERSION ,则使用以下步骤:
    • If the environment variable MSBUILDDEFAULTTOOLSVERSION is set to a ToolsVersion that exists, use it.如果环境变量MSBUILDDEFAULTTOOLSVERSION设置为存在的ToolsVersion ,请使用它。
    • If DefaultOverrideToolsVersion is set in MSBuild.exe.config , use it.如果在MSBuild.exe.config设置了DefaultOverrideToolsVersion ,请使用它。
    • If DefaultOverrideToolsVersion is set in the registry, use it.如果在注册表中设置了DefaultOverrideToolsVersion ,请使用它。
    • Otherwise, use the current ToolsVersion .否则,使用当前的ToolsVersion

There is a chance you are building with the wrong MSbuild.exe;您有可能使用错误的 MSbuild.exe 进行构建; do the compile in Visual Studio (where it works) and check the logs in Output.在 Visual Studio(它工作的地方)中进行编译并检查输出中的日志。 There should be something like:应该有类似的东西:

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

Make sure you are using the MSBuild.exe in that Bin directory, in my case;确保你在那个 Bin 目录中使用 MSBuild.exe,在我的例子中;

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSbuild.exe

Microsoft.Net.Compilers 不起作用,但从 Nuget 安装DotNetCompilerPlatform起作用了。

I'm just guessing that you have code with string interpolations and don't have the proper build tools .我只是猜测您有带有字符串插值的代码并且没有合适的构建工具

My solution was 2 part:我的解决方案是两部分:

1) Using Visual Studio 2015, select your web project, click the Project menu, select "Enable C# 6" 1)使用Visual Studio 2015,选择你的web项目,点击Project菜单,选择“Enable C# 6”

2) I needed to add the following to the end of my production web.config, just before the configuration closing tag. 2)我需要将以下内容添加到我的生产 web.config 的末尾,就在配置结束标记之前。 Note that version numbers may change in the future, but the key is to look for similar text in your development web.config after enabling in step 1 and make sure it gets transferred to production.请注意,版本号将来可能会更改,但关键是在步骤 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>

您必须使用 msbuild 版本 15。

I had this problem because I used the wrong msbuild path.我遇到这个问题是因为我使用了错误的 msbuild 路径。 I was using C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe which corresponds to old versions of Visual Studio and Frameworks, now I use Visual Studio 2019, so the correct path to build is C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exe or C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe depending on the architecture you want to build.我使用的是C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\MSBuild.exe对应旧版本的 Visual Studio 和 Frameworks,现在我使用的是 Visual Studio 2019,所以正确的构建路径是C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\MSBuild.exeC:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community\\MSBuild\\Current\\Bin\\amd64\\MSBuild.exe取决于您要构建的架构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM