Saturday, 1 September 2012

Attaching to IIS easily

By default in Visual Studio it takes several actions to attach to IIS (w3wp.exe). You have to open the attach to process window (Ctrl+Alt+P), select the option to show all processes, find w3wp.exe in the list and select attach; all of this is performed multiple times a day.

This post describes how to create a macro in visual studio then create a button for it and/or a shortcut key.


Creating the macro

The first thing you need to do is to add the macro via macro IDE.

Then add a new module, I have chosen to name mine DebuggingMacros.

Finally add the code shown below into the module, save the module and close the macro IDE.

Public Module DebuggingMacros

    Public Sub AttachToWebServer() 

       Dim AspNetWp As String = "aspnet_wp.exe"

        Dim W3WP As String = "w3wp.exe" 


        If Not (AttachToProcess(W3WP)) Then

           If Not AttachToProcess(AspNetWp) Then

                System.Windows.Forms.MessageBox.Show("Can't find web server process")
            End If

        End If 

    End Sub


    Public Function AttachToProcess(ByVal ProcessName As String) As Boolean

        Dim Processes As EnvDTE.Processes = DTE.Debugger.LocalProcesses

        Dim Process As EnvDTE.Process

        Dim ProcessFound As Boolean = False 


        For Each Process In Processes

           If (Process.Name.Substring(Process.Name.LastIndexOf("\") + 1) = ProcessName) Then

                Process.Attach()

                ProcessFound = True

            End If

        Next 

 

        AttachToProcess = ProcessFound 

    End Function 



End Module

Creating the button

Once the macro has been created, we can make a button by clicking on customize as shown.

Click on the commands tab, select the toolbar radio button and then select the menu you would like to add the button to. I have chosen to add my button to the debug menu. When you have done this click on the add command button.

Select macros on the left hand side and then select the macro we created earlier.

Finally got to modify selection as shown and rename the button.


My button

This is a demonstration of my button on the debug toolbar which I named "Attach to IIS".


Creating the shortcut key

To add a shortcut key to attach to IIS, first click on options as shown.

The final step is to find the macro, I did this by entering "webserver" in the "show commands containing" field. Then entered your shortcut (mine is Ctrl+Shift+W) in the shortcut key field and hit the assign button.


Summary

By following the steps detailed above you should now be able to attach to the web server with one key combination or one click. Saving you time by removing the need to manually select the process. The same rules as attaching to w3wp through the attach to process screen still exist, that is that w3wp.exe must be running first in order to attach to it.

No comments:

Post a Comment