This is the third post on the series create a macro to Attach to Local IIS. The last modification I want to implement is the ability to list all the w3wp.exe active processes, if more than one process is present, I want it to show a list of all IIS processes and let the user choose the list of processes to attach to. Clearly if I have no w3wp.exe active processes, or I have only one, there is no need to bother the user to choose the single available process.
Since I need to show a user interface from a macro and I’m not allowed to insert form into Visual Studio Macros Editor, I should create a Windows Forms programmatically. This is quite annoying, but creating simple interfaces without a designer is quite simple, so it is the right approach to maintain the code in a simple macro. First of all I find all IIS processes and add info about these processes inside a ListView.
To keep a reference to the original EnvDTE80.Process2 object I add the reference to the Tag property of the ListViewItem. Now that I have a ListView populated with all IIS processes, I need to do some check to verify if we have processes available.
As you can see the code is really simple, if I have no valid worker process, I simply return, but if I have a single IIS worker process available, I’ll attach the debugger to it without requiring user input. If I have more than one worker process available I need the user to select the list of the processes he want to attach to.
The code is really simple, because it is used only to create a valid Windows Forms interface to show the ListView with Processes info and an OK Button at the bottom to permit to the user to confirm the selection. Now you can start multiple IIS worker process, press the shortcut assigned to this macro and here is the result.
Figure 1: The list of all available IIS processes
The User Interface is minimal, I show the process Id and the user that is actually running the process; this last information is really important, because I usually use a different user for each different product I’m working to, so if I need to attach to all IIS processes of a certain product, I immediately can identify all IIS processes that are running with that credential and attach the debugger to the right list of processes.
When the user press OK button, I can iterate through all selected items of the ListView, cast the Tag property to EnvDTE80.Process2 and attach the debugger to the process. As you can see I use the overload version of the Attach2() process that accepts no parameter; this permits me to avoid to specify if I want to debug asp.net 2.0 or 4.0 code, and I can let the debugger choose the right one. Remember that you cannot debug at the same time ASP.NET 4.0 and earlier version, so you could not choose two worker process that are running two different version of the framework.
This post shows clearly that Visual Studio Macro are powerful, yet very simple to use and they can save you huge amount of time automating recurring operations.
A file with the complete macro can be downloaded from here. (http://www.codewrecks.com/files/attachtoiis.txt)