Auto Attach Debugger to Any Process in Visual Studio

For SharePoint developers frustrated with attaching IIS processe to the debugger or are accustomed to debugging a webservice hosted in IIS, this article will help you speed up your development work or at least save a few minutes/seconds.

Do you know that any process you need to repeat again and again can be automated in Visual Studio? How? Have you heard of Macros? Yes this is the key for winning this game and get a step ahead of other developers.

Use the following to understand how to attach a process to a debugger:

  1. Go to Tools -> Macros -> MacroExplorer
  2. Add a new Item in MyMacros (a new module file). Name it as you wish.
  3. Paste the following code in it. Make sure to change the module name to your new macro file name.

    Option Strict Off
    Option Explicit Off
    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports System.Diagnostics

    Public Module IISDebugAttach

        Sub AttachDebuggerToIIS()
            Dim processToAttachTo As String = "w3wp.exe"

            If Not AttachToProcess(processToAttachTo) Then
                MsgBox(processToAttachTo & " is not running")
            End If
        End Sub

        Function AttachToProcess(ByVal processName As String) As Boolean

            Dim proc As EnvDTE.Process
            Dim attached As Boolean
            For Each proc In DTE.Debugger.LocalProcesses
                If (Right(proc.Name, Len(processName)) = processName) Then
                    attached = True
                End If

            Return attached
        End Function
    End Module

  4. Build it (optional).
  5. Again go to Tools-> Customize
  6. Click on the Keyboard button at the bottom
  7. Now type and your macro will be shown in the filtered list; select it.
  8. Assign a short key to it and set apply.

Now forget about the headache of attaching to an IIS process every time. Just press your shortcut key. (Make sure you're running your Visual Studio with administrator privilege.)

Have fun. Cheers!!