Attach to local IIS Macro evolution

I’ve already blogged about the creation of a macro that permits you to attach to IIS worker process pressing a key combination. This approach does not work if you have multiple w3wp.exe processes that are actually running into your machine, because the macro only attach to the very first of them.

A simple and stupid solution is to attach to every w3wp.exe processes that is actually running in the system :), it is somewhat a patch solution, but it is really easy to implement, because it requires only a minor modification of the code generated by the Macro Recorder.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
Sub AttachToIISProcess()
Try
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(2) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("T-SQL")
dbgeng(1) = trans.Engines.Item("Managed (v2.0, v1.1, v1.0)")
Dim processes As EnvDTE.Processes = dbg2.GetProcesses(trans, "")
For Each proc As EnvDTE80.Process2 In processes
If (proc.Name.EndsWith("w3wp.exe")) Then
proc.Attach2(dbgeng)
End If
Next
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Sub

The change is in lines 8-13, in the first version I use the Item Index property of the EnvDTE.Processes object to grab the first instance of the process named w3wp.exe, in this smarter version I iterate through each process, and attach to every process that ends with the string w3wp.exe. With this little modification I’m able to attach to every IIS process, a capability that is useful when you are debugging multiple sites / services and you need to use multiple worker processes.

Alk.