错误代码MSB4018: "CopyRefAssembly"任务意外失败。

4

这个错误似乎是随机发生的,我在互联网上进行了搜索,但没有找到任何有用的结果。如果我清除bin、obj和.vs目录,它会在几次构建后修复,然后这个错误又会出现。

这个错误看起来是随机发生的。我已经在互联网上搜索过,但没有找到任何有用的结果。如果我清空bin、obj和.vs目录,它会在几次编译后解决问题,但之后这个错误会再次出现。
1>------ Build started: Project: GridViewOnly, Configuration: Debug Any CPU ------
1>Failed to check the content hash of the destination ref assembly 'C:\Users\myuser\source\repos\GridViewOnly\bin\Debug\net5.0-windows\ref\GridViewOnly.dll'. It will be overwritten.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018: The "CopyRefAssembly" task failed unexpectedly.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018: System.InvalidOperationException: The "CopyRefAssembly" task has not registered its resources. In order to use the "TaskLoggingHelper.FormatResourceString()" method this task needs to register its resources either during construction, or via the "TaskResources" property.
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(String resourceName, Object[] args)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.Build.Shared.ErrorUtilities.VerifyThrowInvalidOperation(Boolean condition, String resourceName, Object arg0)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.Build.Utilities.TaskLoggingHelper.FormatResourceString(String resourceName, Object[] args)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.Build.Utilities.TaskLoggingHelper.LogErrorWithCodeFromResources(String messageResourceName, Object[] messageArgs)
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.CodeAnalysis.BuildTasks.CopyRefAssembly.Copy()
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.CodeAnalysis.BuildTasks.CopyRefAssembly.Execute()
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
1>C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4413,5): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()
1>Done building project "GridViewOnly.csproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
2个回答

7
在您的解决方案的csproj文件中将ProduceReferenceAssembly设置为false。
<PropertyGroup>
 <TargetFramework>net5.0</TargetFramework> 
 <ProduceReferenceAssembly>false</ProduceReferenceAssembly> 
</PropertyGroup>

你可以添加一个示例,例如: net5.0 false - jspinella
对于 .NET 5.0+,Rahul 的是答案。Matt 的也可能有效,但我认为问题特别出现在 .NET 5 创建的那个 refs 文件夹中。 - jspinella

5

我使用配置管理器创建了一个 x64 配置,问题似乎已经解决了。自从几天前我这样做以来,我没有再遇到这个问题。之前,我只是使用默认的 AnyCPU 配置。我最终删除了它,因为我知道我不需要它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接