构建REST Core解决方案时遇MSB4036错误:找不到FormatLocalizedString任务
错误详情
Error MSB4036 The "FormatLocalizedString" task was not found. Check the following: 1.) The name of the task in the project file is the same as the name of the task class. 2.) The task class is "public" and implements the Microsoft.Build.Framework.ITask interface. 3.) The task is correctly declared with
in the project file, or in the *.tasks files located in the "C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64" directory. REST.Core.Host C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\v17.0\TypeScript\Microsoft.TypeScript.targets 69

可行解决方案
修复TypeScript构建工具
打开Visual Studio Installer,找到你的VS2022专业版实例,点击「修改」,切换到「单个组件」标签,找到「TypeScript构建工具」选项:- 若未勾选,直接勾选后点击「修改」完成安装;
- 若已勾选,先取消勾选并应用,再重新勾选安装,强制修复损坏的组件文件。
替换损坏的Microsoft.TypeScript.targets文件
从同版本的正常VS2022环境中,复制C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Microsoft\VisualStudio\v17.0\TypeScript\Microsoft.TypeScript.targets文件,覆盖你机器上的对应文件。如果没有可用的正常环境,删除该文件后通过VS Installer修复TypeScript组件,让系统自动重新生成这个文件。清理构建缓存并重新生成
- 删除项目目录下的
bin和obj文件夹; - 打开VS,依次点击「工具」→「NuGet包管理器」→「管理解决方案的NuGet包」→「设置」→「清除所有NuGet缓存」;
- 重启VS后重新生成整个解决方案。
- 删除项目目录下的
验证MSBuild任务注册
检查C:\Program Files\Microsoft Visual Studio\2022\Professional\MSBuild\Current\Bin\amd64目录下的.tasks文件,确认是否存在FormatLocalizedString任务的注册项。如果缺失,通过修复VS安装来恢复这些注册文件,不要手动编辑.tasks文件避免引入新问题。
内容的提问来源于stack exchange,提问作者winston dsouza

