新建了一个ASP.NET Core 5.0的Web API项目,当使用断点调试Host.CreateDefaultBuilder(args)时,进入该函数后查看中间变量的值,报错Evaluation is not allowed: The thread is not at a GC-safe point
。在群里问了也没人回应,可能没有遇到过这个问题吧。
一.解决问题的过程
1.Visual Studio 2022调试
首先想到的是可能Rider不行,换成Visual Studio 2022试试,所以就查到了文献[1],发现需要pdb文件,还需要设置符号什么的,觉得太麻烦了。
2.Rider调试
然后又换成了Rider,既然调试看不到中间变量的值,那就直接Console.WriteLine()出来,结果Console在当前上下文中根本就不存在,如下:
二.最终的解决方法
1.YouTrack上的解决方法
继续在网上查找,发现在JetBrains的官方YouTrack上[2][3],Evgeny Terekhin在2022年5月30日给出了一个解决方法:
SET COMPLUS_ZapDisable=1 NGen off (CLR)
SET COMPLUS_JitMinOpts=1 Disable as much JIT optimizations as possible (CoreCLR)
SET COMPlus_TieredCompilation=0 No tiered JIT, only do one pass (CoreCLR)
SET COMPLUS_ReadyToRun=0 Don't do netcore's analog to NGen (CoreCLR)
大概的意思是对CLR做了设置,先不管设置的什么了,抱着试试看的心态。配置launchSettings.json文件:
"environmentVariables": {"ASPNETCORE_ENVIRONMENT": "Development","COMPLUS_ZapDisable": "1","COMPLUS_JitMinOpts": "1","COMPlus_TieredCompilation": "0","COMPLUS_ReadyToRun": "0"
}
完成的配置文件如下所示:
2.调试ASP.NET Core
打上断点启动调试后,神奇般的发现也可以查看中间变量了。如下:Rider调试源码还是比Visual Studio方便很多,不需要pdb文件,也不需要设置什么符号,只需要进入要调试的函数中打上断点,然后启动调试即可。
参考文献:
[1]Debugging External Sources with Visual Studio:https://devblogs.microsoft.com/visualstudio/debugging-external-sources-with-visual-studio/
[2]Debugger: Evaluation is not allowed: The thread is not at a GC-safe point:https://youtrack.jetbrains.com/issue/RIDER-40357
[3]When debugging, variables don't display all of their data, instead we get 'Evaluation is not allowed: The thread is not at a GC-safe point':https://youtrack.jetbrains.com/issue/RIDER-10885/When-debugging-variables-dont-display-all-of-their-data-instead-we-get-Evaluation-is-not-allowed-The-thread-is-not-at-a-GC-safe