What Was I Thinking?

Follies & Foils of .NET Development

  Home  |   Contact  |   Syndication    |   Login
  74 Posts | 0 Stories | 191 Comments | 0 Trackbacks

News

Archives

Post Categories

Check These Out

Gurus

Friday, October 30, 2009 #

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.

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati