如果多个文件具有相同的名称,Visual Studio 断点会在错误的源文件(或同时出现多个文件)中中断 [英] Visual Studio breakpoints break in the wrong source file (or multiple files simultaneously) if multiple files have the same name
问题描述
在我正在处理的团队项目中,如果解决方案中存在另一个同名文件,则在文件中设置断点(例如 IdeasController.cs
)将导致调试器行为不稳定.我已经在几个开发人员的工作站上重现了这个问题.
In a team project I'm working on, setting a breakpoint in a file (say IdeasController.cs
) will lead to erratic debugger behaviour if there's another file with the same name in the solution. I've reproduced the problem on several developers' workstations.
我在我们的 Web API 的 IdeasController.cs
中设置了一个断点:
I set a breakpoint in IdeasController.cs
in our Web API:
另一个名为 IdeasController.cs
的文件存在于我们单独的 MVC 4 Web 项目中.在下面的屏幕截图中,调试器显示了 Api->IdeasController
源代码,但高亮行与 Web->IdeasController
的代码结构匹配.断点是重复的,其中一个位于注释块的中间.
Another file called IdeasController.cs
exists in our separate MVC 4 web project. In the screenshot below, the debugger shows the Api->IdeasController
source code, but the line highlight matches the code structure of Web->IdeasController
. The breakpoint is duplicated, with one of them in the middle of a comment block.
断点窗口同时显示两个文件中的断点:
The Breakpoint window shows the breakpoint in both files simultaneously:
在某些工作站上,调试器会逐步执行正确的行(无论行高亮如何);在其他人身上,它会愉快地穿过不相关的行(包括注释和空格).我猜这取决于它选择显示的源文件.
On some workstations the debugger steps through the correct lines (regardless of the line highlight); on others it cheerfully steps through irrelevant lines (including comments and whitespace). I'm guessing this depends on which source file it chooses to display.
我浏览了互联网.当调试文件(*.pdb
)、源文件和编译代码不匹配时,似乎会出现这种问题.有很多可能的原因:重复的文件名(这会混淆调试器[5])、过时的项目构建文件、无效的解决方案缓存或不正确的构建配置.
I've trawled the Internet. This kind of problem seems to occur when there's a mismatch between the debug file (*.pdb
), the source file, and the compiled code. There are a lot of possible causes: duplicate file names (which can confuse the debugger[5]), outdated project build files, invalid solution cache, or incorrect build configuration.
这些是我找到并尝试过的解决方案:
These are the solutions I've found and tried:
- 检查了我的构建配置.
- Checked my build configuration.
- 确保项目不是在发布模式下构建的.
- 确保我们 没有启用代码优化.
- 确保项目的调试模块已正确加载.(开始调试项目并检查
Debug
>Windows
>Modules
.两个程序集都已列出,未优化,并且符号状态为Symbols已加载".)
- Made sure the project isn't built in release mode.
- Made sure we don't have code optimization enabled.
- Made sure the project's debug module was loaded correctly. (Started debugging the project and checked
Debug
>Windows
>Modules
. Both assemblies are listed, not optimized, and have a symbol status of "Symbols loaded".)
- 关闭 Visual Studio 并删除解决方案缓存文件 (
*.suo
).[1] - 删除了每个项目的构建输出(
bin
和obj
文件夹).(供将来参考:在 Windows 资源管理器中打开解决方案文件夹并在搜索框中键入:type:folder AND (name:=bin OR name:=obj)
". - 删除了程序集缓存文件夹(
C:Documents and Settings<user>Local SettingsApplication Datadl3
).[2][3]
- Closed Visual Studio and deleted the solution cache file (
*.suo
).[1] - Deleted each project's build output (the
bin
andobj
folders). (For future reference: open the solution folder in Windows Explorer and type this in the search box: "type:folder AND (name:=bin OR name:=obj)
". - Deleted the assembly cache folder (
C:Documents and Settings<user>Local SettingsApplication Datadl3
).[2][3]
这些都没有任何效果.我可以重命名其中一个文件(不重命名类)以临时解决该问题,但这远非理想.
None of these had any effect. I can rename one of the files (without renaming the class) to temporarily work around the problem, but that's far from ideal.
我最新的 Google 搜索的第 14 页.建议将不胜感激.:)
Page 14 of my latest Google search. Suggestions would be much appreciated. :)
推荐答案
如果没有更好的选择,你可以在代码中设置断点:
If no better alternatives exist, you could put the breakpoint in code:
System.Diagnostics.Debugger.Break();
只是不要忘记之后删除它......
Just don't forget to remove it afterwards...
这篇关于如果多个文件具有相同的名称,Visual Studio 断点会在错误的源文件(或同时出现多个文件)中中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!