views:

234

answers:

3

I've got a WCF host application which gets started by an executable with some dynamic parameters.

Now I want to debug the this application but since it's getting started by the other tool VS.NET won't load it in debug mode.

Is it possible to write some DEBUG only code to force it execute this process in DEBUG mode so it'll hit the break points.

Currently it's using Process.Start() to start the host application.

Attaching it to the debugger every time is not an option, I'm looking for a more practical solution.

+1  A: 

Try adding this line in your WCF app:

System.Diagnostics.Debugger.Launch();

If the app is not running under the debugger, you'll get a window asking if you want to debug the application.

TskTsk
It doesn't load the source code and related projects though, it works just like attaching the debugger which is quite hard. Also somehow I can't choose the current VS.NET instance to debug which would solve the problem.
dr. evil
Did you compile them with debug symbols and are they available alongisde the WCF app's dlls?
TskTsk
Yes I do. 1234567890
dr. evil
You mentioned that it doesn't let you choose the current VS.net instance to debug... were you running the current instance in DEBUG mode? It should attach to the one that is running if it is in DEBUG mode...
TskTsk
The trick was I need 2 instances of the same project, it can't attach it to the same VS.NET instance.
dr. evil
A: 

VS.NET macros are pretty powerful. I think you could pretty easily script the attach.

Alex
+1  A: 

You can do it without modifying the source code. Open the auto-started project in Visual Studio and set a breakpoint. Start Regedit.exe and add a key to "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Image File Execution Options" with the exact same name as your .exe. Add a new string value named "Debugger", set it to "c:\Windows\system32\vsjitdebugger.exe". An example .reg file:

REGEDIT4
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\ConsoleApplication1.exe]
"Debugger"="\"c:\\Windows\\system32\\vsjitdebugger.exe\""

Now, when your app starts the process, the JIT debugger prompt shows up. Select the Visual Studio instance that has the auto-started project loaded. Execution will stop at the breakpoint. Note that you usually have to change the focus back to VS yourself.

Mike Stall has warned that this trick doesn't work for managed only debugging. I cannot reproduce that, it works fine in VS2008 SP1.

Hans Passant
+1 You can also toggle this registry setting using gflags.exe. See http://stackoverflow.com/questions/2053724/how-to-debug-with-external-program-launched-from-bat-not-an-exe-in-vs2005/2053865#2053865
Nick Guerrera