How to debug class library that called from external app?

Sergejs picture Sergejs · Dec 2, 2012 · Viewed 43.8k times · Source

There is an external workflow that executes C# scripts and is able to work with DLL files(my class library).

Is it possible to attach debug to my class library project so breakpoint will hit once this WF will call it?

Thanks

Answer

T.J. Crowder picture T.J. Crowder · Dec 6, 2012

Yes, you can do this with Visual Studio. You have two options:

Configure your project to start the external program

  1. Open your DLL project.

  2. On the properties for the project, go to the Debug tab.

  3. Choose Start external program and give the path of the external program that will call your DLL, along with any command-line arguments you may need to supply, and the working directory if that's relevant.

  4. Save the project.

  5. Set breakpoints in your code where you want them.

  6. Press F5 to start debugging. (At this point, your breakpoints will say that they won't be hit because the symbols aren't loaded. Don't worry about that for now.)

  7. Do whatever you do to make the external application load your library and run your code.

Visual Studio will detect the module load, load the symbols, and stop on the breakpoint.

Attach to an existing process

If you can't start the process but instead have to attach to a process that's already running, you can do that too:

(Side note: If you're using the "Express" edition of Visual Studio, I don't think it has this feature, but I'm not certain about that. It's easy enough to tell: You'll either have the menu item mentioned on Step 4 below or not.)

  1. Make sure the process is running.

  2. Open your DLL project.

  3. Set your breakpoints, etc.

  4. From the Debug menu, choose Attach to process...

  5. In the resulting dialog box, find the process in the list, highlight it, and click Attach.

  6. Visual Studio will go into debug mode. (At this point, your breakpoints will say that they won't be hit because the symbols aren't loaded. Don't worry about that for now.)

  7. Do whatever you do to make the external process load and run your code.

Visual Studio will detect the module load in the external process, load your symbols, and stop on your breakpoint.


N.B. In both cases, if the external process loads your DLL from somewhere other than the bin/Debug folder of your project, you must make sure you copy the DLL to that other location every time you build it (you can set that up to happen automatically in the project options). Otherwise, Visual Studio won't be able to detect that the DLL being loaded is the one you're trying to debug.