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: Sub AttachToIISProcess()

   2:      Try

   3:          Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger

   4:          Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")

   5:          Dim dbgeng(2) As EnvDTE80.Engine

   6:          dbgeng(0) = trans.Engines.Item("T-SQL")

   7:          dbgeng(1) = trans.Engines.Item("Managed (v2.0, v1.1, v1.0)")

   8:          Dim processes As EnvDTE.Processes = dbg2.GetProcesses(trans, "")

   9:          For Each proc As EnvDTE80.Process2 In processes

  10:              If (proc.Name.EndsWith("w3wp.exe")) Then

  11:                  proc.Attach2(dbgeng)

  12:              End If

  13:          Next

  14:      Catch ex As System.Exception

  15:          MsgBox(ex.Message)

  16:      End Try

  17:  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.


Published by

Ricci Gian Maria

.Net programmer, User group and community enthusiast, programmer - aspiring architect - and guitar player :). Visual Studio ALM MVP

One thought on “Attach to local IIS Macro evolution”

Comments are closed.