In VS2010, is there a way to know which application pool a given w3wp.exe is serving, to then decide to attach the debugger to?

后端 未结 5 1980
情深已故
情深已故 2021-01-07 14:03

So I\'m debugging some websites (one from trunk, one from branch) running locally, in separate apppools. I have trunk and branch solutions open in two VS instances. I\'d l

5条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 14:24

    Below is my core macro. Write a few one-line subs calling it, like AttachToW3wp("DefaultAppPool") naming each app pool you’re interested in, and make buttons and hotkeys for them.

        Private Sub AttachToW3wp(ByVal appPoolName As String)
        Const processName As String = "w3wp.exe"
        Dim userName As String = String.Format("IIS APPPOOL\{0}", appPoolName)
    
        Try
            Dim debugger As EnvDTE90.Debugger3 = CType(DTE.Debugger, EnvDTE90.Debugger3)
            'debugger.DetachAll()
    
            Dim transport As EnvDTE80.Transport = debugger.Transports.Item("Default")
            Dim qualifier As String = Environment.MachineName '= My.Computer.Name
            Dim engines(3) As EnvDTE80.Engine
            engines(0) = transport.Engines.Item("Managed")
            engines(1) = transport.Engines.Item("Script")
            engines(2) = transport.Engines.Item("T-SQL")
    
            Dim successMessage As String = String.Empty
            For Each process As EnvDTE80.Process2 In debugger.GetProcesses(transport, qualifier)
                With process
                    Dim fi As New System.IO.FileInfo(.Name)
                    If fi.Name = processName AndAlso (String.Compare(.UserName, 0, userName, 0, Len(userName), True) = 0) Then
                        If .IsBeingDebugged Then Throw New Exception(String.Format("{0} {1} is already attached to a debugger.", processName, userName))
    
                        process.Attach2(engines)
                        successMessage = String.Format("Attached to {0} for {1} ({2})", processName, userName, .ProcessID)
    
                        Exit For
                    End If
                End With
            Next
    
            If successMessage = String.Empty Then
                Throw New Exception(String.Format("{0} {1} not found.", processName, userName))
            Else
                Trace.WriteLine(successMessage)
            End If
    
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    

提交回复
热议问题