Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Macro to attach debugger to correct w3wp process.

0.00/5 (No votes)
16 Feb 2013 1  
Macro to attach visual studio debugger to w3wp apppool hosting your webservice.

Introcution

The below macro can be used to attach Visual Studio debugger with the correct w3wp process to debug web application.

Background

While working on webservices we often need to debug server side code using visual stuido and to do that we need to attach the corresponding apppool which is hosting our webservice.

This can be achieved using Visual Studio Attach to Process window where we can select the correct w3wp process to debug the code.

Most of the time developers select all w3wp process available in Attach to Process window. As the information about the apppool hosting the webservice is missing in the Attach to Process window.

With this macro we can attache debugger to the correct w3wp apppool which is hosting our webservice.

Also by creating macro icon in Visual Studio toolbar, bebugger can be attach with a single click.

Below is the link which would help you in the creation of the macro and creating a shortcut for it in visual studio.

http://blogs.msdn.com/b/jannemattila/archive/2008/10/30/attaching-debugger-to-w3wp-exe-using-nice-and-easy-keyboard-shortcut.aspx

Using the code

In MacroID add the below code in your Module. Replace your apppool name in the below code as shown below.

If (input.ToString().Contains("<nameOfapppool>")) Then.

To know the apppoolname in which your application is running run the below command in command prompt after going to directory C:\Windows\System32\inetsrv\

appcmd.exe list wp

the name of your apppool will appear like as shown in the below image.

Code:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics


Public Module Module1
    Public Sub AttachW3WP()
        Const lngCancelled_c As Long = 0
        Dim strCmd As String
        strCmd = "C:\Windows\System32\inetsrv\appcmd.exe list wp"
        If Len(strCmd) = lngCancelled_c Then
            Exit Sub
        End If
        CommandLine(strCmd)
    End Sub


    Public Function CommandLine(ByVal command As String) _
    As Boolean
        On Error GoTo Err_Hnd
        Dim WshShell, oExec, input
        WshShell = CreateObject("WScript.Shell")
        oExec = WshShell.Exec(command)
        input = ""
        Dim flag = True
        Do While flag
            If Not oExec.StdOut.AtEndOfStream Then
                input = oExec.StdOut.ReadLine
                If (input.ToString().Contains("<nameOfapppool>")) Then
                    Dim attached As Boolean = False
                    Dim proc As EnvDTE.Process
                    Dim procID As String
                    Dim tempString As String
                    procID = input.ToString().Split("(")(0).Split(" ")(1).Replace("""", "")
                    For Each proc In DTE.Debugger.LocalProcesses
                        If (proc.ProcessID = procID) Then
                            proc.Attach()
                            attached = True
                            flag = False
                            Exit For
                        End If
                    Next
                End If
            Else
                flag = False
            End If
        Loop
        CommandLine = True
        Exit Function
Err_Hnd:
        CommandLine = False
    End Function
End Module

Points of Interest

I found macros to be very helpful and I always try to use them to save time.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here