As I stated previously, the capability of Visual Studio to use Macro is probably one of the less known feature and one of the coolest. Thanks to macro you can automate standard task you do in VS to speed-up your everyday coding experience. Since I work heavily with WCF services hosted in IIS and Web Application, quite often I need to attach the debugger to my local IIS instance.

Untitled

This requires me to navigate to Debug->Attach to process, show the list of processes, find the w3wp.exe process, attach, confirm the dialog, bla bla bla. It wouldn’t be beautiful to attach to local IIS with the simple combination of keys? The solution is really simple, just select Tools->Macros->Record temporary macro, then attach to local IIS and stop macro recording. You will end up with a Macro that you can simply modify and add to some standard Macro module.

You can simply open macro editor (tools->Macros->Macros IDE), then create a new module (I’ve called StandardCustomMacro) and then paste the recorded macro to attach to IIS.

   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 proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "").Item("w3wp.exe")

   9:         proc2.Attach2(dbgeng)

  10:     Catch ex As System.Exception

  11:         MsgBox(ex.Message)

  12:     End Try

  13: End Sub

As you can verify, attaching to a process is really a simple task, now I can simply customize the keyboard binding using Tools->Customize.

sdfasdfasdfasdf

Figure 1: Just filter for macros, identify your new macro and assign some key binding like CTRL+SHIFT+ALT+I

Now you can press the combination of buttons you setup in Figure 1 and you attach automatically to IIS.

Alk.

Tags:

2 Responses to “Visual Studio Macro–Attach To Local IIS”

  1. Hi Gian Maria,
    it seems a nice solution but it doesn’t work with FW 4.0 and 64 bit applications.

    I think u should changed this part of code

    dbgeng(1) = trans.Engines.Item(“Managed (v2.0, v1.1, v1.0)”)

    and add the support for “Managed (v4.0.30319), x64″

    Another cool feature is open a window with all w3wp active process on local server (I could have 10 iis istallation with 10 different application pool).

    Do u think is easy to implement?

  2. You are right for the changing of the line, :) I’ve recorded while attaching to a 3.5 project, :) . I’ll give a look on how to solve the situation where you have multiple w3wp active process ;)