Those of us SharePoint developers know all too well how to debug our solutions. If you're building a new Web Part, in order to debug it, you have to manually attach the Visual Studio debugger to the W3SVC.EXE process by selecting Debug -> Attached to Process... and then selecting one or more instances of the W3SVC.EXE process, and click Attach. This is tedious... and repetitive.
Hmm... repetitive... can't that be automated? Anyone who's seen one of my "Don't be a Tool..." talks in the Florida community knows that I'm a huge proponent of automating anything and everything possible. Recently I spotted a post on the PowerShell blog titled the Philosophy of Automation (a killer post, and a recommended read) where the author, Jeffery Snover, nails a great quote on why I love tools & automating stuff:
"Rather by increasing the number of things that we don't have to think about, we can spend our thinking on NEW things and solving NEW problems and then transforming those things into things that we no longer have to think about."
- Philosophy of Automation; Jeffery Snover (MSFT PowerShell Blog)
Anyway... getting back to the point of this post...
This whole debugging thing in SharePoint drives me batty. If I want to debug my Web Part or event receiver, I have to manually attach to the W3SVC.EXE processes (or the specific one for my Application Pool if I want to take the time to find it). If I'm debugging a custom timer job, I have to attach to the OWSTIMER.EXE process. Well, these repetitive processes can be automated in Visual Studio!
Follow the same process I outlined in my post When "F5" debugging doesn't cut it and manually attaching to processes takes too long... last May on creating a new macro. Here are three I use in my SharePoint development. One attaches to all W3SVC.EXE processes, the other attaches to the OWSTIMER.EXE process, and finally, as a bonus, there's another that attaches to the MbUnit GUI which I use for my unit testing (here's the text version to download and copy into your macro editor):
1: Imports System
2: Imports EnvDTE
3: Imports EnvDTE80
4: Imports System.Diagnostics
5:
6: Public Module Debuggers
7: Function AttachToProcess(ByVal processName As String) As Boolean
8: Dim proc As EnvDTE.Process
9: Dim attached As Boolean
10: For Each proc In DTE.Debugger.LocalProcesses
11: If (Right(proc.Name, Len(processName)) = processName) Then
12: proc.Attach()
13: attached = True
14: End If
15: Next
16:
17: Return attached
18: End Function
19:
20: Sub AttachDebuggerToMbUnit()
21: Dim processToAttachTo As String = "MbUnit.GUI.exe"
22:
23: If Not AttachToProcess(processToAttachTo) Then
24: MsgBox(processToAttachTo & " is not running")
25: End If
26:
27: End Sub
28:
29: Sub AttachDebuggerToWssTimerService()
30: Dim processToAttachTo As String = "OWSTIMER.EXE"
31:
32: If Not AttachToProcess(processToAttachTo) Then
33: MsgBox(processToAttachTo & " is not running")
34: End If
35: End Sub
36:
37: Sub AttachDebuggerToIIS()
38: Dim processToAttachTo As String = "w3wp.exe"
39:
40: If Not AttachToProcess(processToAttachTo) Then
41: MsgBox(processToAttachTo & " is not running")
42: End If
43: End Sub
44: End Module