MSB4018: 任务意外失败

当任务因未处理的异常而失败时,将发出此错误。 这通常是任务中存在 bug 的标志。

当在一个未准备好的环境中运行任务时,可能会导致错误 MSB4018,例如,当任务有一个 x86 依赖项但在 64 位 MSBuild 环境中运行时。 这可以表现为 System.DllNotFoundException

S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018: The "AnyCPUTaskWithPInvoke" task failed unexpectedly.
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018: System.DllNotFoundException: Unable to load DLL 'Native32BitLibrary.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at AnyCPUTaskWithPInvoke.AnyCPUTaskWithPInvoke.NativeMethod()
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at AnyCPUTaskWithPInvoke.AnyCPUTaskWithPInvoke.OtherMethod() in S:\BitnessInMSBuild\AnyCPUTaskWithPInvoke\AnyCPUTaskWithPInvoke.cs:line 19
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at AnyCPUTaskWithPInvoke.AnyCPUTaskWithPInvoke.Execute() in S:\BitnessInMSBuild\AnyCPUTaskWithPInvoke\AnyCPUTaskWithPInvoke.cs:line 12
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
S:\BitnessInMSBuild\ShowErrors.proj(6,7): error MSB4018:    at Microsoft.Build.BackEnd.TaskBuilder.<ExecuteInstantiatedTask>d__26.MoveNext()

若要解决此问题,请确保任务在任务的 UsingTask 元素中表示依赖于它所需要的运行时环境。