Nothing new in this post… just seems ever few weeks/months you talk to someone that wasn’t aware you could do this. The last person I spoke to had a hard time finding this so I’m posting it for their, and now your, edification.
MCMS 2002 and WSS v2 Web Part developers know that you can’t just hit F5 (see footer) to build and enter into your debugging environment without any extra configuration in Visual Studio. In order to debug your process, you do the following:
- Build your solution
- If it’s not already started, fire off one request to your solution/project/whatever to get the (in the case of IIS 5.x) ASP.NET worker process to start up or (in the case of IIS 6.0, w3wp.exe)
- Using Attach the Visual Studio debugger to the process
- Start debugging
That takes no fewer than 4 mouse clicks (VS Debug Menu > attach to process… > select aspnet_wp.exe/w3wp.exe > click OK). I’m not much of a mouse guy… much prefer shortcuts with my keyboard. The less I have to take my hands off the keyboard while working on a project, the more productive I can be. Not to mention, you could do this many times in the course of working on a project… say you jump into the debugger once every three minutes. On my relatively new laptop, that takes about 6 seconds. Over the course of a day, that adds up (for those few souls who actually work 8hrs a day, that’s just under 3 minutes a day). OK, so it’s not THAT much of an impact… but it’s a repetitive process… can’t that be automated? How do you get around this annoying use of the mouse?
Setup a macro to automatically attach to the desired process, set a shortcut to trigger that macro, and another shortcut to detach from the process. The following assumes you’re working in Visual Studio 2005. The steps are similar if not identical in VS 2003. VS 2005 just has sexier screenshots (ok, just better looking). :P
First, we’ll create the macro:
- Right-click MyMacros in the Macro Explorer tool window (bring up the Macro Explorer by pressing ALT + F8 ) and select New Module….
- Give your module a name, I used VSDebugger.
- Right-click your new module, in my case VSDebugger, and select New macro.
- Change the name of the macro to something that makes more sense; I used AttachTo_ASPNETWP.
- Replace the default macro added to your module with the following code. I’ve heavily commented it so you can understand what’s going on:
' This routine attaches to the ASP.NET worker processSub AttachTo_ASPNETWP() Dim attached AsBoolean = False Dim proc As EnvDTE.Process Dim processToAttachTo AsString ' name of the process to attach to processToAttachTo = "aspnet_wp.exe" ' iterate through all processes running on the local machine ForEach proc In DTE.Debugger.LocalProcesses ' if the last [X] characters of the process name = name of the process... If (Right(proc.Name, Len(processToAttachTo)) = processToAttachTo) Then ' attach to the process proc.Attach() ' set a flag that we've attached to the process & exit the for loop attached = True ExitFor EndIf Next EndFor ' if macro didn't find process running, notify user If attached = False Then MsgBox(processToAttachTo & " is not running") EndIf EndSub
Next, we need to create a keyboard shortcut:
- In Visual Studio, open the Options dialog (Tools | Options).
- Select the Keyboard node under the Environment category.
- In the Show commands containing: textbox, enter VSDebugger (or the name you chose for your module) to find the command in the listbox.
- To digress for a second… why in the world did the VS IDE developers (1) give us 10,000 items in such a small listbox (only see 5 items at a time) and/or (2) not make the Options dialog resizable? Man, that seems so obvious to fix.
- Place the cursor in the Press Shortcut keys: input box and type the key combination you’d like to use to fire your macro off. As you can see in the screenshot, I use a two step command. I press CTRL + SHIFT + P , release, and then press A . Why? The first part is easy to remember as it’s binding to a process P . The second command is Attaching to the process:
Repeat the same process to create a shortcut to detach from the process. In this case, search for detachall… I used the key combination CRTL + SHIFT + P , D (so A attaches, D detaches).
Now you’re good to go! After building your solution ( CRTL + SHIFT + B ), hit your key combination to attach to the process.