I’ve been spending a lot of time lately debugging through some web-hosted applications. Some of these applications are hosted by Visual Studio’s own internal web server (Cassini), and others are hosted by my local instance of IIS.
Web projects hosted locally get automatic attached to the VS debugger when you press F5. Projects hosted under IIS do not. In order to debug IIS hosted projects, you must attach the Visual Studio debugger to the IIS worker process (aspnet_wp.exe or w3wp.exe) manually.
Traditionally, you attach by selecting Attach To Process from the Debug menu, scrolling the list of processes until you find the worker process and then clicking “Attach”.
Thanks to this handy macro I found by HologramX at snipplr.com, I’m able to attach with a single keystroke:
To add this macro to your environment, Tools > Macro IDE > Add new item to MyMacros > Select Module, Name 'AttachToWebServer' > Copy/paste code.
Code Snippet
- Imports System
- Imports EnvDTE80
- Imports System.Diagnostics
-
- Public Module AttachToWebServer
-
- Public Sub AttachToWebServer()
- Dim AspNetWp As String = "aspnet_wp.exe"
- Dim W3WP As String = "w3wp.exe"
- If Not (AttachToProcess(AspNetWp)) Then
- If Not AttachToProcess(W3WP) Then
- System.Windows.Forms.MessageBox.Show(String.Format("Process {0} or {1} Cannot Be Found", AspNetWp, W3WP), "Attach To Web Server Macro")
- End If
- End If
- End Sub
-
- Public Function AttachToProcess(ByVal ProcessName As String) As Boolean
- Dim Processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses
- Dim Process As EnvDTE.Process
- Dim ProcessFound As Boolean = False
- For Each Process In Processes
- If (Process.Name.Substring(Process.Name.LastIndexOf("\") + 1) = ProcessName) Then
- Process.Attach()
- ProcessFound = True
- End If
- Next
- AttachToProcess = ProcessFound
- End Function
- End Module
To set as a keyboard shortcut go to Tools > Options > Keyboard, and search for the name of the macro (AttachToWebServer) and then assign a keyboard shortcut.
IIS-Attached Debugging is now one keystroke away.