简体   繁体   English

VS2012 断点没有被击中

[英]VS2012 Breakpoints are not getting hit

I have a class that looks like this:我有一个看起来像这样的课程:

public class MyService
{
    private MyService(){}
    public static string GetStuff()
    {
        var stuffDid = new MyService();
        return stuffDid.DoStuff();
    }
    private string DoStuff()
    {
        //do stuff
    }
    //other private helpers

}

Obviously I left a lot out, but that's the general shell.显然我遗漏了很多,但这是一般的外壳。

Now, I have a unit test:现在,我有一个单元测试:

[Test]
public void MyTest()
{

    var results = MyService.GetStuff();
}

I set breakpoints on my unit test, and I can see that results has data.我在我的单元测试上设置了断点,我可以看到results有数据。 However, I set breakpoints literally all over MyService and nothing gets hit unless I put them on a curly brace.但是,我实际上在MyService设置了断点,除非我将它们放在花括号上,否则不会受到任何影响。 Which I can't understand since results has data, my return statements in MyService should be getting hit, right?我无法理解,因为results有数据,我在MyService return语句应该被击中,对吧?

Am I missing something?我错过了什么吗? Did I completely forgot the most basic rules of something?我是不是完全忘记了一些最基本的规则? How come nothing in MyService gets hit?为什么MyService没有任何东西被击中? And if I manually step into it with F11 , it just hops around and doesn't even go through every line like I would expect.如果我使用F11手动进入它,它只会跳来跳去,甚至不会像我期望的那样遍历每一行。 Also when I step through manually I tend to hit certain code after I should have hit it originally.此外,当我手动逐步执行时,我倾向于在我最初应该点击它之后点击某些代码。 And any switch statements seem to default to whatever the first option is, even though the value being switched should CLEARLY enter a different case .并且任何switch语句似乎都默认为第一个选项,即使被切换的值应该明确输入不同的case

I've even tried making MyService constructor public and taking away all static methods, and it still doesn't work.我什至尝试将MyService构造函数MyService publicMyService所有static方法,但它仍然不起作用。

My Tests and 'Core' code are in the same solution, but different projects( Test and Core , respectively).我的测试和“核心”代码在同一个解决方案中,但不同的项目(分别是TestCore )。 Other tests don't have an issue hitting break points in Core , only this on particular test (the only test that is testing MyService ).其他测试在Core没有遇到断点问题,只有在特定测试(测试MyService的唯一测试)时才会出现此问题。

I've deleted my PDB files and cleaned solution.我已经删除了我的 PDB 文件并清理了解决方案。 Still nothing.依然没有。

Some ideas.一些想法。

  1. Make sure it's a debug build and not release确保它是一个调试版本而不是发布
  2. Turn off optimizations in your project properties if they are on关闭项目属性中的优化(如果它们处于打开状态)
  3. Try inserting Debugger.Break() in your code instead of a breakpoint in VS尝试在代码中插入Debugger.Break()而不是 VS 中的断点
  4. Make sure breakpoints are enabled (Debug->Windows->Breakpoints toolbar), and breakpoint symbol should be solid确保断点被启用(Debug->Windows->Breakpoints 工具栏),并且断点符号应该是实心的
  5. Execute your application.执行您的应用程序。 Load Debug->Window->Modules window.加载调试->窗口->模块窗口。 Check your assembly to see if symbols are loaded.检查您的程序集以查看符号是否已加载。 It may give a relevant status message if not.如果没有,它可能会给出相关的状态消息。

Have you been adjusting the date on your computer at all?你有没有在电脑上调整过日期? This can really screw up a build process.这真的会搞砸构建过程。 If so, delete all your obj/bin folders manually and recompile.如果是这样,请手动删除所有 obj/bin 文件夹并重新编译。

It could be beacuse you are only debugging 1 project not both Test and Core这可能是因为您只调试 1 个项目,而不是TestCore

You can set VS to debug mutiple projects at once, you can do this by right-click your solution > Properties > Common Properties > StartUp Project您可以将 VS 设置为一次调试多个项目,您可以通过right-click your solution > Properties > Common Properties > StartUp Project

Here you can set "Multiple Startup Projects"在这里你可以设置“多个启动项目”在此处输入图片说明

Just set both Core and Test to start.只需设置CoreTest即可开始。 This may solve your issue.这可能会解决您的问题。

It turns out this was related to Code Coverage being on.事实证明,这与代码覆盖率有关。

Turning it off fixed the issue.关闭它解决了这个问题。

You can find out how to disable code coverage by following below link您可以通过以下链接了解如何禁用代码覆盖

Disable code coverage 禁用代码覆盖

I have a very specific scenario that resulted in the apparent issue of "Breakpoint not being hit".我有一个非常具体的场景,导致“未命中断点”的明显问题。

Since no other answers here mentioned it, I will add mine in the chance that it will help someone who had the same issue.由于此处没有其他答案提到它,因此我将添加我的答案,以帮助遇到相同问题的人。

The solution, in my case, was silly, and with as much LINQ as I use I should have figured this out sooner.就我而言,该解决方案很愚蠢,而且使用了尽可能多的 LINQ,我应该早点弄明白这一点。 When running a method that returns an IEnumerable, where the return statements contained within are actually yield return statements, then that method will not be executed when you call it.当运行一个返回 IEnumerable 的方法时,其中包含的 return 语句实际上是yield return语句,那么在调用它时将不会执行该方法。

It will actually be executed when you call another method from that IEnumerable object, such as ToList() or Count() .当您从该 IEnumerable 对象调用另一个方法时,它实际上会被执行,例如ToList()Count() Only then will the method be executed and the breakpoint reached.只有这样才能执行该方法并到达断点。

Just make sure that you have build your assembly with the debugger symbols.只需确保您使用调试器符号构建了程序集。

This option has to be filled with "full":此选项必须填写“完整”:

Right-Click your project containing your code file with the break points not getting hit.右键单击包含代码文件的项目,断点未命中。 Choose "Properties".选择“属性”。

After project properties have been opened, choose "Build" tab.打开项目属性后,选择“构建”选项卡。 Watch out for the "Advanced..."-Buttom at the bottom of the tab page.注意标签页底部的“高级...”-按钮。 (Within the "Output"-Group") (在“输出”-组内)

Click this button and choose "full" for the "Debug info" property.单击此按钮并为“调试信息”属性选择“完整”。 This should be a reason for breakpoints not getting hit.这应该是断点未命中的原因。 Visual studio uses the symbols saved in the pdb-files to find the exact position of the break point. Visual Studio 使用保存在 pdb 文件中的符号来查找断点的确切位置。 If these files are not created, no breakpoints are hit.如果未创建这些文件,则不会命中任何断点。 Maybe you disabled the creation of these files in order to tidy up your project file structure.也许您禁用了这些文件的创建以整理您的项目文件结构。 This was a situation I recognized that I need these files.这是我认识到我需要这些文件的情况。

I've recently had the same problem and was smashing my head against the wall.我最近遇到了同样的问题,我的头撞在墙上。

The answer turned out to be pretty silly: Somehow my test project got out of sync with the main library project.结果证明答案非常愚蠢:不知何故,我的测试项目与主库项目不同步。 I was building the debug versions of the test and library, but the test project copied the library from the bin/Release folder.我正在构建测试和库的调试版本,但测试项目从bin/Release文件夹复制了库。 I just recreated the project reference and everything was fixed.我刚刚重新创建了项目参考,一切都已修复。

PS It was even criazier: the debugger went inside a library function, but somehow skipped one line in the middle of it. PS它甚至更疯狂:调试器进入了一个库函数,但不知何故跳过了它中间的一行。

You need to make DoStuff static.您需要将 DoStuff 设为静态。

private static string DoStuff()
{
    //do stuff
}

Your code indicates a "service", which could be running as a separate process.您的代码表示一个“服务”,它可以作为一个单独的进程运行。 If that's the case you can have your assembly loaded, so breakpoints would be solid red circles but another copy of the assembly, running in a separate process is actually handling the requests.如果是这种情况,您可以加载程序集,因此断点将是红色实心圆圈,但程序集的另一个副本,在单独的进程中运行实际上是在处理请求。

  • check Task Manager for possible offenders (processes that may be hosting your service).检查任务管理器是否有可能的违规者(可能托管您的服务的进程)。 Kill them while debugging to confirm the calls fail.在调试时杀死它们以确认调用失败。
  • Try using Debugger.Break();尝试使用 Debugger.Break();
  • Create a debug log file, upon loading output to the log the entry process and assembly names.创建一个调试日志文件,在将输出加载到日志时,输入进程和程序集名称。 Make sure your log is either a different file each time to avoid async access issues.确保您的日志每次都是不同的文件,以避免异步访问问题。

This sounds like the pdb files are not updated in your test sandbox.这听起来像是测试沙箱中的 pdb 文件没有更新。

1) Ensure that you are in debug mode. 1) 确保您处于调试模式。

2) Can you try and include a deployment item for the pdb files explicitly? 2)您可以尝试明确包含 pdb 文件的部署项吗?

  • You said that you can attach a debug point in your test project.你说你可以在你的测试项目中附加一个调试点。
  • Once you have hit the debug point in your test project , check to make sure the pdb files with the latest time stamp is present in the Out folder of your sandbox.一旦到达测试项目中的调试点,请检查以确保沙箱的 Out 文件夹中存在具有最新时间戳的 pdb 文件。

3) If 1 and 2 fail , I have found that sometimes visual studio requires a restart :) 3)如果1和2失败,我发现有时visual studio需要重启:)

  1. Clean the solution and rebuild and also do the start up project.清理解决方案并重建并执行启动项目。

  2. Can you please have a quick look at the BUILD > Configuration Manager, just to sure what are the configuration properties are set up.您能否快速查看一下 BUILD > Configuration Manager,以确定设置了哪些配置属性。 If it is development then you might have to adjust the Project Properties -> click advance setting -> change debug info to 'full' in [output tab].如果是开发,那么您可能需要调整项目属性 -> 单击高级设置 -> 在 [输出选项卡] 中将调试信息更改为“完整”。

  3. you can also follow step two even it is not development mode即使不是开发模式,您也可以按照第二步操作

I had this happen in a 1 project out of 25 which were all in the same solution.我在 25 个项目中的 1 个项目中发生了这种情况,这些项目都在同一个解决方案中。 The other projects honored breakpoints but this 1 didn't.其他项目尊重断点,但这个 1 没有。 I removed the project from the solution (delete, not unload) which broke all references to it and then added it back to the solution and that worked!我从解决方案中删除了该项目(删除,而不是卸载),这破坏了对它的所有引用,然后将其添加回解决方案并且有效!

If that doesn't work you may want to recreate the problem project from scratch and add that new project to the solution.如果这不起作用,您可能需要从头开始重新创建问题项目并将该新项目添加到解决方案中。

The best explanation I have for why this worked apart from pure luck is that we migrated projects from one version of VS to another many, many times over the years and maybe one of those migrations caused this problem.除了纯粹的运气之外,我对为什么这行得通的最好解释是,我们多年来多次将项目从一个版本的 VS 迁移到另一个版本,也许其中一个迁移导致了这个问题。

You could try to add a Thread.Sleep(5000) in GetStuff method and use Attach to Process您可以尝试在GetStuff方法中添加一个Thread.Sleep(5000)并使用 Attach to Process

Visual Studio > Tools > Attach To Process and see if breakpoints below that line gets hit. Visual Studio > 工具 > 附加到进程并查看该行下方的断点是否被击中。

如果它处于发布模式,则将其切换到调试模式。

I know from experience that Visual Studio does not have a clear method of debugging services, especially Windows services.我从经验中知道,Visual Studio 没有明确的调试服务的方法,尤其是 Windows 服务。 Try adding some code to GetStuff to print to a text file, this way you at least know the code is getting hit.尝试将一些代码添加到 GetStuff 以打印到文本文件,这样您至少知道代码正在被命中。 When creating services I often fall back on this method for testing.在创建服务时,我经常使用这种方法进行测试。

I ran into a similar issue.我遇到了类似的问题。 It turned out that for me it was a bad migration from VS2010 to VS2012 with the *.testrunconfig file.事实证明,对我来说,使用*.testrunconfig文件从 VS2010 到 VS2012 的迁移很糟糕。 I deleted the old one and set up a new one to resolve the issue.我删除了旧的并设置了一个新的来解决问题。

VS behaves exactly the way you described (not hitting the breakpoints, hitting the code you're not expecting to hit when stepping through) when it uses .pdb file which was generated using the source code somehow different from the code which is used upon debugging.当 VS 使用使用源代码生成的 .pdb 文件时,VS 的行为与您描述的完全相同(不击中断点,击中您不希望击中的代码) . I can't guarantee that this is your case, but I've observed such behaviour many times when I needed to step into the code which was supplied as a pre-built library which was generated against an older/different code with same filenames/symbols.我不能保证这是你的情况,但是当我需要进入作为预构建库提供的代码时,我已经多次观察到这种行为,该代码是针对具有相同文件名的旧/不同代码生成的/符号。

Maybe your Test project is referencing an older Core binary, rather than the Core (source code) project?也许您的Test项目引用的是较旧的Core二进制文件,而不是Core (源代码)项目?

Try re-adding the reference in your Test project:尝试在您的测试项目中重新添加引用:

Go to your Test project and remove the reference to the Core project.转到您的Test项目并删除对Core项目的引用。

Now select the References folder and right click it and select the menu option to add a new reference.现在选择 References 文件夹并右键单击它并选择菜单选项以添加新引用。 When in the Reference Manager dialog, make sure you are selecting Solution and then Projects on the left.在“参考管理器”对话框中,确保您选择了Solution ,然后选择了左侧的Projects Then in the middle of the Reference Manager dialog, select (check) the Core project.然后在 Reference Manager 对话框的中间,选择(选中) Core项目。

Try debugging again and see if that helps.再次尝试调试,看看是否有帮助。

Some more stuff to try:还有一些东西可以尝试:

  • Check if the loaded symbols match the debugged executable:检查加载的符号是否与调试的可执行文件匹配:
    Open a VS command prompt and cd to the directory where the executable you debug resides.打开 VS 命令提示符并 cd 到您调试的可执行文件所在的目录。 Then do a dumpbin /PDBPATH:VERBOSE MyServiceExecutable.exe and scan the output for "PDB age mismatch" (Ref: http://msdn.microsoft.com/en-us/library/44wx0fef.aspx )然后执行dumpbin /PDBPATH:VERBOSE MyServiceExecutable.exe并扫描“PDB age mismatch”的输出(参考: http : //msdn.microsoft.com/en-us/library/44wx0fef.aspx

  • Not sure about VS 2012, but older versions of VS had a bug where the wrong source file would be displayed, provided that you have two source files in your project that have the same name, even when they are located in different folders.不确定 VS 2012,但旧版本的 VS 有一个错误,即会显示错误的源文件,前提是您的项目中有两个具有相同名称的源文件,即使它们位于不同的文件夹中。 So if your project contains another source file with the same name, see if renaming one of them helps.因此,如果您的项目包含另一个同名的源文件,请查看重命名其中一个文件是否有帮助。 ( Update: Seems VS 2012 is affected too .) 更新:似乎 VS 2012也受到影响。)

Silly me the Test Project wasn't set to be built:愚蠢的我没有设置测试项目:

在此处输入图片说明

This one's pretty obscure :这个很晦涩:

Make sure you don't have two virtual directories with different App Pools pointing to the same physical location on your harddrive.确保您没有两个具有不同应用程序池的虚拟目录指向硬盘驱动器上的相同物理位置。 During development this is something that can sometimes happen for testing, or by mistake.在开发过程中,这有时会发生在测试中,或者是错误的。

I'm not 100% clear on the technicalities but I had two AppPools and two virtual directories and no breakpoint was ever hit because I presume the physical path was somehow mapped in IIS/Visual Studio to the other apppool and not the one that was actually executing.我对技术问题不是 100% 清楚,但我有两个 AppPools 和两个虚拟目录,并且没有遇到断点,因为我认为物理路径在 IIS/Visual Studio 中以某种方式映射到另一个应用程序池,而不是实际的那个执行。

I have the same issue.我有同样的问题。 Maybe my solution will help you to solve your problem.也许我的解决方案会帮助你解决你的问题。 Just in "Attach to Process" for option "Attach to" select value "Avtomatic: Native code".只是在“附加到进程”选项“附加到”选择值“Avtomatic:本机代码”。 Best regards.最好的祝福。

图像

First try rebuilding your project by right mouse click the project > Rebuild If that doesn't work, try a clean of the project (right mouse click on the project > clean)首先尝试通过鼠标右键单击项目> 重建来重建您的项目 如果这不起作用,请尝试清理项目(鼠标右键单击项目> 清理)

If that didn't work check this:如果这不起作用,请检查:

Right mouse click your project
select [Properties]
select the [Build] tab
make sure [Define DEBUG constant] and [Define TRACE constant] are checked
Click the [Advanced] button at the bottom of the Build tabpage
Make sure that [Debug Info:] is set to [full]
Click [OK] and rebuild the project ;-)

Hope that works for you!希望对你有用! (step 6 generates the .pdb files, these are the debugging symbols) (第 6 步生成 .pdb 文件,这些是调试符号)

To debug step-by-step, you must do two things.要逐步调试,您必须做两件事。 First you must set the break point, then you must attach the debugger to the process running your code.首先必须设置断点,然后必须将调试器附加到运行代码的进程。 If you are running IIS Express and you have 64 bit machine, then you need to attach iisexpress.exe that is running your code.如果您正在运行 IIS Express 并且您有 64 位机器,那么您需要附加运行您的代码的iisexpress.exe If you press CTRL + ALT + P, you'll get to the attach to process window.如果您按 CTRL + ALT + P,您将进入附加到进程窗口。 After attaching, the break point should be hit if the code matches.附加后,如果代码匹配,则应命中断点。

In Unit Tests, I was not hitting breakpoints, and realized I was Running the test and not Debugging the test.在单元测试中,我没有遇到断点,并意识到我正在运行测试而不是调试测试。 At the top of the Test Explorer are the options "Run All", "Run Failed", "Run Passed", etc. When you Run a test, breakpoints are not hit.测试资源管理器的顶部是“全部运行”、“运行失败”、“运行通过”等选项。当您运行测试时,不会命中断点。 To Debug a test, in the Test Explorer right click on the test or group of tests and select Debug Selected Tests.要调试测试,请在测试资源管理器中右键单击测试或测试组,然后选择调试选定的测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM