KoenV

Quick Attach and Detach Debugger

Here's a trick I often use in Visual Studio to speed up the trial-and-error process.

A disadvantage of using an ASP.NET Web Application Project instead of an ASP.NET Web Site is that you cannot edit code behind files (or basically any code file in your web project) while you are debugging. So to edit just minor things there, you need to stop the debugger (which closes the browser typically), edit, compile and start the debugger again, which could take some time.

I have written (recorded and adjusted) a small VS macro that attaches the debugger for me. It will actually look if the VS built-in webserver is active. If not it will attach to IIS.

Sub AttachDebugger()

  Try

    Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger

    Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")

    Dim dbgeng(1) As EnvDTE80.Engine

    dbgeng(0) = trans.Engines.Item("Managed")

 

    Dim proc2 As EnvDTE80.Process2

    Try

      proc2 = dbg2.GetProcesses(trans, "").Item("WebDev.WebServer.EXE")

    Catch ex As Exception

      proc2 = dbg2.GetProcesses(trans, "").Item("w3wp.exe")

    End Try

 

    proc2.Attach2(dbgeng)

  Catch ex As System.Exception

    MsgBox(ex.Message)

  End Try

End Sub

Copy this in one of your Macro modules and it should compile. The script could be a bit different on Windows XP, because the ASP.NET process is called "aspnet_wp" there...

And then assign shortcuts to your macro. For detaching you don't need a macro, it's a built-in command. My shortcut's are Ctrl+Shift+A for attaching and Ctrl+Shift+D for detaching.

Now you can detach your debugger (leaving the browser open), edit the code, build it (only build what you changed), attach and refresh the browser...

Comments

Guy Harwood said:

This is a good tip. many thanks

# February 14, 2008 5:53 AM

Remco Ros said:

Thanks for sharing, saves us some clicks :-)

# February 14, 2008 6:02 AM

Joe Chung said:

There's already a keyboard shortcut for this:  F5.

You can set your Web Application Project's properties to use IIS instead of the Development Web Server.

# February 14, 2008 6:52 AM

koevoeter said:

Joe Chung wrote:
There's already a keyboard shortcut for this:  F5.
You can set your Web Application Project's properties to use IIS instead of the Development Web Server.

F5 is what you use of course when you start debugging, but then when you want to adjust small bits, you don't need to stop and restart everything. Basically leaving the brower open is sometimes a lot of help (because the state is not gone).

And I created it for both the built-in web server as IIS because I have applications that specifically require IIS, so doesn't matter which project I'm working on. Obviously in that case you need to change the project debugging settings...

# February 14, 2008 7:24 AM

mkellogg said:

My personal favorite for attaching to IIS in Vista is the following key combination.

Alt+D, P, W, 3, <Return>

It's like second nature now.

Shift+F5 to detach.

# February 14, 2008 10:50 AM

koevoeter said:

Yeah, like the method HumanSendKeys() :o)

# February 14, 2008 11:37 AM

AndrewSeven said:

This seems to be related to the addiction many developers seem to have to the F5 or "Start Debugging" button.

If you are using IIS, you don't need to be debugging all the time, you only need a debugger when you debug.

I've used a macro before, but I prefer to lever the Start Debugging button.

If your browser is not set to reuse shortcuts, you can  browse the site with one browser "main" instance. When you realize there is a bug, you can use F5 to start debugging. This will open a new window (I use a dummy html page as my debugging start page). Ignore the new window and return to the "main" one.

# February 14, 2008 11:55 AM

Reflective Perspective - Chris Alcock » The Morning Brew #33 said:

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #33

# February 15, 2008 2:38 AM

koevoeter said:

Andrew:

I think you're right. In Visual Studio there's no real difference between functional (UI) testing and debugging. You could see the start without debugging (Ctrl+F5) as a "testing" function, but I think it needs more than changing the name...

# February 15, 2008 4:02 AM

This Week in VS.net #1 : Mostly Programming Stuff said:

Pingback from  This Week in VS.net #1 : Mostly Programming Stuff

# February 15, 2008 9:24 AM

jakew said:

My current project is a bit of a pain. I'm having to dance between 2 to 4 instance of Visual Studio while

# October 8, 2008 1:32 PM

Chris King said:

Great! It also attaches in debug only mode. This is good because I had another macro that was attaching in T-SQL too. When it did that and I switched between debugging the client and the server I would lose the server stack. In managed only debugging mode I can switch between processes.

# February 27, 2009 8:01 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)