VS2012 Breakpoints are not getting hit

2019-01-21 22:13发布

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 results has data. However, I set breakpoints literally all over MyService and nothing gets hit unless I put them on a curly brace. Which I can't understand since results has data, my return statements in MyService should be getting hit, right?

Am I missing something? Did I completely forgot the most basic rules of something? How come nothing in MyService gets 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 switch statements seem to default to whatever the first option is, even tho the value being switched should CLEARLY enter a different case.

I've even tried making MyService constructor public and taking away all static methods, and it still doesnt work.

Edit: My Tests and 'Core' code are in the same solution, but different projects(Test and 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.

Edit 2:

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

24条回答
该账号已被封号
2楼-- · 2019-01-21 22:35

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.
查看更多
forever°为你锁心
3楼-- · 2019-01-21 22:35

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

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

查看更多
祖国的老花朵
4楼-- · 2019-01-21 22:36

This sounds like the pdb files are not updated in your test sandbox.

1) Ensure that you are in debug mode.

2) Can you try and include a deployment item for the pdb files explicitly?

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

3) If 1 and 2 fail , I have found that sometimes visual studio requires a restart :)

查看更多
对你真心纯属浪费
5楼-- · 2019-01-21 22:38

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

查看更多
孤傲高冷的网名
6楼-- · 2019-01-21 22:41

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

查看更多
成全新的幸福
7楼-- · 2019-01-21 22:42

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.

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.

查看更多
登录 后发表回答