You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用dotnet.exe 8.0运行单元测试时遭遇IOException问题求助

dotnet test 执行时出现 SocketException(10054),测试全部通过但进程返回退出码1

Azure DevOps 流水线执行日志

Info: .NET Core SDK/runtime 2.2 and 3.0 are now End of Life(EOL) and have been removed from all hosted agents. If you're using these SDK/runtimes on hosted agents, kindly upgrade to newer versions which are not EOL, or else use UseDotNet task to install the required version.
"C:\Program Files\dotnet\dotnet.exe" test C:\Agents\AG3-1\_work\115\s\src\My.Project.UnitTests\My.Project.UnitTests.csproj --logger trx --results-directory C:\Agents\AG3-1\_work\_temp --configuration Release --logger trx "--collect:Code Coverage"
  Determining projects to restore...
  All projects are up-to-date for restore.
  My.Project -> C:\Agents\AG3-1\_work\115\s\src\My.Project\bin\Release\net8.0\My.Project.dll
  My.Project.UnitTests -> C:\Agents\AG3-1\_work\115\s\src\My.Project.UnitTests\bin\Release\net8.0\My.Project.UnitTests.dll
Test run for C:\Agents\AG3-1\_work\115\s\src\My.Project.UnitTests\bin\Release\net8.0\My.Project.UnitTests.dll (.NETCoreApp,Version=v8.0)
VSTest version 17.12.0 (x64)

Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
 ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Flush()
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.SocketCommunicationManager.WriteAndFlushToChannel(String rawMessage) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs:line 413
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestSender.SendAfterTestRunEndAndGetResult(ITestMessageEventHandler runEventsHandler, Boolean isCancelled) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/DataCollection/DataCollectionRequestSender.cs:line 155
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass20_0.<AfterTestRunEnd>b__0() in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 155
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.InvokeDataCollectionServiceAction(Action action, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 288
Results File: C:\Agents\AG3-1\_work\_temp\svc_AG3-1_ag3_2025-03-31_02_34_10.trx

Passed!  - Failed:     0, Passed:    12, Skipped:     0, Total:    12, Duration: 1 s - My.Project.UnitTests.dll (net8.0)

[error]Error: The process 'C:\Program Files\dotnet\dotnet.exe' failed with exit code 1

Result Attachments will be stored in LogStore
Run Attachments will be stored in LogStore
##[warning].NET 5 has some compatibility issues with older Nuget versions(<=5.7), so if you are using an older Nuget version(and not dotnet cli) to restore, then the dotnet cli commands (e.g. dotnet build) which rely on such restored packages might fail. To mitigate such error, you can either: (1) - Use dotnet cli to restore, (2) - Use Nuget version 5.8 to restore, (3) - Use global.json using an older sdk version(<=3) to build
Info: Azure Pipelines hosted agents have been updated and now contain .Net 5.x SDK/Runtime along with the older .Net Core version which are currently lts. Unless you have locked down a SDK version for your project(s), 5.x SDK might be picked up which might have breaking behavior as compared to previous versions.
##[error]Dotnet command failed with non-zero exit code on the following projects : [
  'C:\\Agents\\AG3-1\\_work\\115\\s\\src\\My.Project.UnitTests\\My.Project.UnitTests.csproj'
]
Async Command Start: Publish test results
Publishing test results to test run '2337437'.
TestResults To Publish 12, Test run id:2337437
Test results publishing 12, remaining: 0. Test run id: 2337437
Async Command End: Publish test results
Finishing: Run tests

本地复现日志

System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host..
 ---> System.Net.Sockets.SocketException (10054): An existing connection was forcibly closed by the remote host.
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.IO.BufferedStream.Flush()
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.SocketCommunicationManager.WriteAndFlushToChannel(String rawMessage) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/SocketCommunicationManager.cs:line 413
   at Microsoft.VisualStudio.TestPlatform.CommunicationUtilities.DataCollection.DataCollectionRequestSender.SendAfterTestRunEndAndGetResult(ITestMessageEventHandler runEventsHandler, Boolean isCancelled) in /_/src/Microsoft.TestPlatform.CommunicationUtilities/DataCollection/DataCollectionRequestSender.cs:line 155
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.<>c__DisplayClass20_0.<AfterTestRunEnd>b__0() in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 155
   at Microsoft.VisualStudio.TestPlatform.CrossPlatEngine.DataCollection.ProxyDataCollectionManager.InvokeDataCollectionServiceAction(Action action, ITestMessageEventHandler runEventsHandler) in /_/src/Microsoft.TestPlatform.CrossPlatEngine/DataCollection/ProxyDataCollectionManager.cs:line 288
Results File: C:\temp\myName_myPcName_2025-03-31_15_23_42.trx
  My.Project.UnitTests test failed with 1 error(s) (6.5s)
    C:\Program Files\dotnet\sdk\9.0.200\Microsoft.TestPlatform.targets(48,5): error MSB6006: "dotnet.exe" exited with code 1.

Test summary: total: 12, failed: 0, succeeded: 12, skipped: 0, duration: 6.5s
Build failed with 1 error(s) in 10.3s

关键观察

  • 问题覆盖多个单元测试项目,包括Web应用、Azure Function、类库等不同类型.NET项目
  • 间歇性出现,并非每次必现,偶尔可正常执行
  • 本地环境中,重启后首次测试成功,后续测试均失败,直至再次重启
  • 回退Microsoft.NET.Test.Sdk版本至17.11.0或更低时,问题消失
  • 错误关联localhost回环套接字连接,已提交微软支持工单
  • 所有测试用例均通过,问题与测试代码无关
  • 仅特定环境(本地电脑+多数Azure构建代理)出现,同事环境无法复现

解决方案建议

  1. 锁定Test SDK版本:在所有测试项目中明确指定Microsoft.NET.Test.Sdk版本为17.11.0或更低,这是已验证有效的临时解决办法
  2. 禁用代码覆盖率收集:移除dotnet test命令中的--collect:Code Coverage参数,错误发生在测试结束后的data collection阶段,跳过该步骤可避免套接字通信问题
  3. 检查安全软件/防火墙:排查本地和代理环境的防火墙、杀毒软件是否拦截了localhost的套接字连接,重启后首次成功可能是安全软件的临时规则导致
  4. 清理Test Platform缓存:删除本地%USERPROFILE%/.nuget/packages/microsoft.net.test.sdk路径下的新版本缓存,或执行dotnet nuget locals all --clear清理所有NuGet缓存
  5. 生成详细诊断日志:添加--diag:test.log参数执行测试,通过日志进一步定位套接字连接的具体端口和进程交互问题,为微软支持提供更详细信息
  6. 排查进程残留:测试前手动终止所有dotnet.exe、vstest.console.exe相关进程,避免旧进程占用端口或资源导致通信失败

内容的提问来源于stack exchange,提问作者VincentZHANG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 14:27:04