Visual Studio macro to attach to w3wp.exe

Use a Visual Studio Macro to hook into w3wp.exe and debug ASP.NET apps faster
December 14 2011

Last weekend I was working on a Media Browser plugin and I came across a post on the Media Browser dev forums showing a Visual Studio macro to ease the pain of debugging plugins, which normally require attaching to the Media Centre host.  (That post is here).

Today at work I had an 'ah ha' moment and thought I should be doing the same for IIS/w3ws, as multiple times a day I'm opening the debug processes, pressing 'w', going down to w3wp.exe and attaching to it do I can debug my ASP.NET applications.  So here it is.

To do the same on your own machine:

  • in Visual Studio to go Tools -> Macros -> Macros IDE...
  • Now right-click on 'My Macros' and select Add -> Add Module...
  • Rename the Module to whatever.
  • Add code from below.
  • Right click on "MyMacros" and select Build then close the Macro IDE
  • In the main VS IDE, go to Tools -> Options -> Keyboard and in "Press shortcut keys:" enter the shortcut keys combination you want to use to run this macro
  • Type 'MyMacros' in to 'Show commands containing:' and select AttachToW3WP and repeat for BuildAndAttachToW3WP

 

I've got two macros because I may not want to rebuild each time.

Public Module VSDebugger

    Public Sub AttachToW3WP()
        Dim attached As Boolean = False
        Dim proc As EnvDTE.Process

        For Each proc In DTE.Debugger.LocalProcesses
            If (Right(proc.Name, 8) = "w3wp.exe") Then
                proc.Attach()
                attached = True
            End If
        Next

        If attached = False Then
            MsgBox("Couldn't find w3wp.exe")
        End If

    End Sub

    Public Sub BuildAndAttachToW3WP()

        DTE.Solution.SolutionBuild.Build(True)

        AttachToW3WP()

    End Sub

End Module

Update: You may experience trouble finding the Macro in Tools->Options->Keyboard. Make sure you open it from the main VS menu, not from within the Macro IDE. Also if you still can't find it, try adding the above code to an existing Module within Samples. A co-worker had this problem when adding the Macro and this fixed it.

Post a comment

comments powered by Disqus