What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

QuickTip: Attach the VS Debugger to IIS’s Worker Process in a single keystroke

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
  1. Imports System
  2. Imports EnvDTE80
  3. Imports System.Diagnostics
  4.  
  5. Public Module AttachToWebServer
  6.  
  7.     Public Sub AttachToWebServer()
  8.         Dim AspNetWp As String = "aspnet_wp.exe"
  9.         Dim W3WP As String = "w3wp.exe"
  10.         If Not (AttachToProcess(AspNetWp)) Then
  11.             If Not AttachToProcess(W3WP) Then
  12.                 System.Windows.Forms.MessageBox.Show(String.Format("Process {0} or {1} Cannot Be Found", AspNetWp, W3WP), "Attach To Web Server Macro")
  13.             End If
  14.         End If
  15.     End Sub
  16.  
  17.     Public Function AttachToProcess(ByVal ProcessName As String) As Boolean
  18.         Dim Processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses
  19.         Dim Process As EnvDTE.Process
  20.         Dim ProcessFound As Boolean = False
  21.         For Each Process In Processes
  22.             If (Process.Name.Substring(Process.Name.LastIndexOf("\") + 1) = ProcessName) Then
  23.                 Process.Attach()
  24.                 ProcessFound = True
  25.             End If
  26.         Next
  27.         AttachToProcess = ProcessFound
  28.     End Function
  29. 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.

Print | posted on Friday, October 30, 2009 8:45 AM | Filed Under [ Visual Studio ]

Feedback

Gravatar

# re: QuickTip: Attach the VS Debugger to IIS’s Worker Process in a single keystroke

Would using their own Cassini be better than using other servers?
6/3/2010 2:10 AM | Boiler Replacement Grants
Gravatar

# re: QuickTip: Attach the VS Debugger to IIS’s Worker Process in a single keystroke

Cassini isn't always an option. For example, if I want to host my service over HTTPS, I'm forced to use IIS.
6/3/2010 7:02 AM | wtfChris
Gravatar

# re: QuickTip: Attach the VS Debugger to IIS’s Worker Process in a single keystroke

Plus you get the real environment as on your hosting site (since there wouldn't be cassini ever). Having exactly same environment on both machines is really nice...
8/15/2012 6:51 AM | Lelala
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: