Andrew Connell [MVP MOSS]
1339 Posts |  39 Articles |  3304 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
PDCBling
SharePoint Quick Links
Article Categories
Archives
Post Categories

View Andrew Connell's profile on LinkedIn

Add to Technorati Favorites

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.

The Problem:

MCMS 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


click to enlarge

click to enlarge

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?

The Solution:

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 commeted it so you can understand what’s going on (or you can get it here):
' This routine attaches to the ASP.NET worker process
Sub AttachTo_ASPNETWP()
  Dim attached As Boolean = False 
  Dim proc As EnvDTE.Process 
  Dim processToAttachTo As String 

  ' name of the process to attach to
 
  processToAttachTo =
"aspnet_wp.exe"
  ' iterate through all processes running on the local machine 
  For Each 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 
      ' appach to the process 
      proc.Attach() 
      ' set a flag that we've attached to the process & exit the for loop 
      attached =
True 
      Exit For 
    End If 
  Next 
 
  ' if macro didn't find process running, notify user 
  If attached = False Then 
    MsgBox(processToAttachTo &
" is not running"
  End If
End Sub

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 develoeprs (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 resizeable? 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 CRTL+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 [A]ttaching to the process P:


click to enlarge

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 (CTRL+SHIFT+B), hit your key combination to attach to the process.

Happy debugging!

[Update 5/10/2006 2:20p EDT]: Technically you CAN debug Web Parts by simply hitting F5, but you have some extra config work to do in Visual Studio and SharePoint. Thanks to Maurice for pointing this out.

posted on Wednesday, May 10, 2006 9:19 AM

Feedback

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 5/25/2006 12:58 AM Stonie

Thanks for the tips! looks like a great macro.

For somereason I can't get this to work on 2003 server?
When I set processToAttachTo = "w3wp.exe" and run the macro it crashes the IDE? any ideas?

Cheers,
Stonie.

# re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 5/25/2006 8:13 AM AC [MVP MCMS]
Gravatar Joe - True... I only take into account you're running a single version of ASP.NET so if there were two, it wouldn't know which one to catch. You could add logic to let the user pick which one or once you've found it, check the assembly itself for a specific version.

Stonie- Hmmm... not sure... have you verified that you have a w3wp.exe process running? Have you stepped through the debugger when it runs?

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 6/5/2006 3:25 PM Tony Bierman (SharePoint Solutio
Nice tip - I use it all the time now.

# re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 5/10/2006 1:05 PM Joe Brinkman
You might have a bit of a problem if you are running both ASP.Net 2.0 and 1.1 projects which will result in multiple ASPNET_WP processes.

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 8/8/2006 2:16 AM Lakshmipathy
Thanks it was a useful tip.I had been searching for a macro like this.

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 8/8/2006 5:13 PM tron
I would recommend removing the Exit For if using Sharepoint Beta 2007 or anything that creates multiple processes- I usually have 3 w3wp.exe processes running at the same time.

Also- I like ALT-A (attach), and ALT-D (detach), any reason not to use those keys?

Anyway thanks, this was very useful.

# re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 8/13/2006 10:56 PM AC [MVP MCMS]
Gravatar Tron - The reason I didn't use the attach/detach keys were because I wanted to attach to a specific process. Also, multiple w3wp.exe processes isn't exclusive to SharePoint 2007, you'll also find it in WSS v2/SPS2003 as well. This is because you usually have multiple app pools running within any SharePoint environment.

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 8/23/2006 2:08 PM Tron
Thanks Andrew- To clarify, I was just saying I like to use ALT-A as a hotkey to use your macro, and alt-D to detach. I've learned that I give up access to the "Data" and "Debug" menus through keyboard. To me, it's no big loss.

I've also added the following code to open a new browser window to my testpage after attaching (I preferred this technique over a Process.Start, because I don't like having my last opened browser window overtaken)

Dim p As System.Diagnostics.Process
p = New System.Diagnostics.Process()
p.StartInfo.FileName = "iexplore.exe"
p.StartInfo.Arguments = "http://sharepoint:63204/sites/test/default.aspx" p.Start()

Hope this helps someone out there!

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 10/25/2006 1:47 PM Darren
Has anyone successfully attached to two processes using macros? I can do it from the "Processes..." dialog, but when I use macros, it only stays attached to the 2nd process to which I attach.

I need two processes because I use NUnit, so I have to attach to that process as well as the asp worker process.

Thanks!

My addie:
gbdarren at hotmale dot com


# Make your SharePoint debugging experience a little less painful 1/25/2007 7:57 AM Andrew Connell [MVP MCMS]
Make your SharePoint debugging experience a little less painful

 VS2003 3/12/2007 3:20 PM Luis LDQ
This works unmodified in VS.NET 2003. Great macro!

 VS2003 3/12/2007 4:00 PM Luis LDQ
Actually, I digress: it works on the first try, but then after you've detached from the process, the macro stops working.

I tried debugging the macro itself, and it is finding the process and executing proc.Attach(), but that statement doesn't seem to do anything.

I've tried detaching by pressing Debug->Detach All, making a button that executes the Samples.VSDebugger.DetachAll macro and clicking that, Anybody else tried with VS.NET 2003?

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 4/12/2007 8:52 AM Stefan
I have the same problem as LDQ. It works fine the first time, but after detaching, when i want to attach again, it doesnt work. Doing it manually by clicking tools/debug process/etc works fine then. But surely I don't want to keep on doing that all the time!

Any idea on this?

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 5/17/2007 6:15 PM Jerry Hiller
This is exactly what i have been looking for. Seems when I use it to attach to w3wp.exe, it does not attach the Managed debugger, if i check the attach to process menu, it shows that its attched to just script and tsql. does anyone know how to make it attach to Managed?

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 7/16/2007 10:17 AM Mo
A quick question to all the experts there:

How do you know which worker process (w3wp.exe) to attach to when there are multiple of them. For example my site has 4 worker process defined in App Pool and am debugging
MOSS workflow code in VS.Net. Ofcourse I can try and attach manually to each of them one by one but that is a pain.

Is there any tool or some technique available there that shows more information about each worker process and what it is running than the default VS.Net attach to process dialog?
Any ideas?

Thanks,
Mo



# re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 7/16/2007 11:09 AM AC [MVP MOSS]
Gravatar Mo-
There is a way to get a list of all the app pools and which w3wp.exe process goes with each one. Open a command prompt and type "cscript.exe %systemroot%\system32\iisapp.vbs" and it will list all app pools and their process ID (PID). You can then use the PID to determine which one to recycle.

However I don't do this. Rather, my my development environment I run all my SharePoint sites off the same app pool which runs under a different identity as all my other app pools. Thus it is easy to see which one to pick. I find I can share app pools in my development environment without a problem as I only need multiple app pools very rarely. Production is a totally different story.

# re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 12/31/2007 9:50 AM james peckham
Gravatar why don't you just use debug action "URL" and choose the URL you're web app is at. It will automagically talk to IIS if it's localhost, and you can setup remote debugging if it's not.

I blogged about it actually, click my link.

 re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 2/22/2008 7:24 AM Vikash
I am using this Macro but I am facing one problem.
Certain section of my code is not debuggable(not able to set break point) as my code type is mixed. Managed as well as native. How can I set this in the Macro before attaching to the exe.

Thanks
Vikash

# re: When "F5" debugging doesn't cut it and manually attaching to processes takes too long... 2/22/2008 9:19 AM AC [MVP MOSS]
Gravatar Vikash-
Sorry... nver had to do this so I'm not sure.

Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 4 and 1 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelivant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2008 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 4-day hands-on and 5-day online WCM classes for developers I offer through the Ted Pattison Group.

Get more information on the WCM courses!


Upcoming Classes
 Hands-on WCM:
 » Irvine, CA
   Aug 4-7, 2008
 » Atlanta, GA
   Sept 22-25, 2008
 Online WCM:
 » July 21-25, 2008

» Register today!

JAX Office Geeks
Jacksonville Office Geeks (JOG)
JOG is a special interest group in Jacksonville, FL dedicated to bringing the local SharePoint commnity together to share tips, tricks, ideas and best practices for developing solutions on the SharePoint platform.

Next meeting details...
When:
 » TBD
Topic:
 » TBD
Speaker:
 » TBD


» Subscribe to the JOG newsletter