C# VS2012 断点没有被击中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14025734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:27:34  来源:igfitidea点击:

VS2012 Breakpoints are not getting hit

c#debuggingvisual-studio-2012visual-studio-debugging

提问by RJP

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 thats 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 resultshas data. However, I set breakpoints literally all over MyServiceand nothing gets hit unless I put them on a curly brace. Which I can't understand since resultshas data, my returnstatements in MyServiceshould be getting hit, right?

我在我的单元测试上设置了断点,我可以看到results有数据。但是,我从字面上设置了断点MyService,除非我将它们放在花括号上,否则不会受到任何影响。我无法理解,因为results有数据,我的return陈述MyService应该受到打击,对吧?

Am I missing something? Did I completely forgot the most basic rules of something? How come nothing in MyServicegets hit? And if I manually step into it with F11, it just hops around and doesnt even go thru every line like I would expect. Also when I step thru manually I tend to hit certain code after I should have hit it originally. And any switchstatements seem to default to whatever the first option is, even tho the value being switched should CLEARLY enter a different case.

我错过了什么吗?我是不是完全忘记了一些最基本的规则?怎么什么都没有MyService被击中?如果我用 手动进入它F11,它只会跳来跳去,甚至不会像我期望的那样通过每一行。此外,当我手动跳过时,我倾向于在我最初应该点击它之后点击某些代码。并且任何switch语句似乎都默认为第一个选项,即使要切换的值应该明确输入不同的case.

I've even tried making MyServiceconstructor publicand taking away all staticmethods, and it still doesnt work.

我什至尝试制作MyService构造函数public并删除所有static方法,但它仍然不起作用。

Edit: My Tests and 'Core' code are in the same solution, but different projects(Testand Core, respectively). Other tests don't have an issue hitting break points in Core, only this on particular test(the only test that is testing MyService.

编辑:我的测试和“核心”代码在同一个解决方案中,但不同的项目(分别为TestCore)。其他测试没有问题创下了破发点Core,仅此于特定的测试(即测试只测试MyService

Edit 2:

编辑2:

I've deleted my PDB files and cleaned solution. Still nothing.

我已经删除了我的 PDB 文件并清理了解决方案。依然没有。

采纳答案by RJP

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

禁用代码覆盖

回答by Sorceri

You need to make DoStuff static.

您需要将 DoStuff 设为静态。

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

回答by Marco Klein

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. 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.

单击此按钮并为“调试信息”属性选择“完整”。这应该是断点未命中的原因。Visual Studio 使用保存在 pdb 文件中的符号来查找断点的确切位置。如果未创建这些文件,则不会命中任何断点。也许您禁用了这些文件的创建以整理您的项目文件结构。这是我认识到我需要这些文件的情况。

回答by Alan

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
  4. Make sure breakpoints are enabled (Debug->Windows->Breakpoints toolbar), and breakpoint symbol should be solid
  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.
  1. 确保它是一个调试版本而不是发布
  2. 关闭项目属性中的优化(如果它们处于打开状态)
  3. 尝试Debugger.Break()在代码中插入而不是在 VS 中插入断点
  4. 确保断点已启用(Debug->Windows->Breakpoints 工具栏),并且断点符号应为实线
  5. 执行您的应用程序。加载调试->窗口->模块窗口。检查您的程序集以查看是否加载了符号。如果没有,它可能会给出相关的状态消息。

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 文件夹并重新编译。

回答by Sten Petrov

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();
  • 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.
  • 检查任务管理器是否有可能的违规者(可能托管您的服务的进程)。在调试时杀死它们以确认调用失败。
  • 尝试使用 Debugger.Break();
  • 创建一个调试日志文件,将输出加载到日志中时,输入进程和程序集名称。确保您的日志每次都是不同的文件,以避免异步访问问题。

回答by sa_ddam213

It could be beacuse you are only debugging 1 project not both Testand 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" enter image description here

在这里你可以设置“多个启动项目” 在此处输入图片说明

Just set both Coreand Testto start. This may solve your issue.

只需设置CoreTest开始。这可能会解决您的问题。

回答by Ark-kun

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/Releasefolder. I just recreated the project reference and everything was fixed.

结果证明答案非常愚蠢:不知何故,我的测试项目与主库项目不同步。我正在构建测试和库的调试版本,但测试项目从bin/Release文件夹中复制了库。我刚刚重新创建了项目参考,一切都已修复。

P.S. It was even criazier: the debugger went inside a library function, but somehow skipped one line in the middle of it.

PS它甚至更疯狂:调试器进入了一个库函数,但不知何故跳过了它中间的一行。

回答by Yakup üny?lmaz

You could try to add a Thread.Sleep(5000)in GetStuffmethod and use Attach to Process

您可以尝试添加一个Thread.Sleep(5000)inGetStuff方法并使用附加到进程

Visual Studio > Tools > Attach To Processand see if breakpoints below that line gets hit.

Visual Studio > 工具 > 附加到进程并查看该行下方的断点是否被击中。

回答by NoobieDude

If it is in release mode switch it to debug mode.

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

回答by vesuvious

I know from experience that Visual Studio does not have a clear method of debugging services, especially Windows services. Try adding some code to GetStuff to print to a text file, this way you at least know the code is getting hit. When creating services I often fall back on this method for testing.

我从经验中知道,Visual Studio 没有明确的调试服务的方法,尤其是 Windows 服务。尝试将一些代码添加到 GetStuff 以打印到文本文件,这样您至少知道代码正在被命中。在创建服务时,我经常使用这种方法进行测试。

回答by JG in SD

I ran into a similar issue. It turned out that for me it was a bad migration from VS2010 to VS2012 with the *.testrunconfigfile. I deleted the old one and set up a new one to resolve the issue.

我遇到了类似的问题。事实证明,对我来说,从 VS2010 到 VS2012 的*.testrunconfig文件迁移很糟糕。我删除了旧的并设置了一个新的来解决问题。