6

我通常在開發網絡應用程序,並且我的工作時間花費了大量的時間來完成「Ctrl + Alt + P」,按進程名排序,然後選擇w3wp.exe來附加我的調試器。如何使Visual Studio宏將調試器附加到w3wp.exe的所有實例?

更糟糕的是,我正在開發跨越多個應用程序池的應用程序,所以我通常會有2或3個w3wp.exe實例,並且不可能知道要附加哪一個實例,所以我通常會結束附加到所有這些,這是過度殺傷力,但工程。

總而言之,這是煩了...

我的同事想出了一個辦法有一個VS宏自動附着到W3WP.EXE(他基本上記錄了這個):

Sub AttachMacro()  
    Try  
    Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger  
    Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")  
    Dim dbgeng(3) As EnvDTE80.Engine  
    dbgeng(0) = trans.Engines.Item("T-SQL")  
    dbgeng(1) = trans.Engines.Item("T-SQL")  
    dbgeng(2) = trans.Engines.Item("Managed")  
    Dim proc2 As EnvDTE80.Process2 = dbg2.GetProcesses(trans, "ENIAC").Item("w3wp.exe")  
    proc2.Attach2(dbgeng)  
    Catch ex As System.Exception  
    MsgBox(ex.Message)  
    End Try  
End Sub 

我真的不確定是否所有這些都是必要的,或者說,我從來沒有爲VS做過宏,我真的不知道從哪裏開始。

會不會有改變這個宏的方式,這樣,而不是其自身附加到的w3wp.exe的例如,它會將自身附加到的w3wp.exe的所有實例?

+0

此功能真的應該是Visual Studio的一部分。任何人想要做出連接問題 - 或檢查現有的問題? – 2013-02-19 07:06:28

回答

8
Sub MacroAttachToAllProcesses() 

    Try 

     Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger 
     Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") 
     Dim dbgeng(3) As EnvDTE80.Engine 

     dbgeng(0) = trans.Engines.Item("T-SQL") 
     dbgeng(1) = trans.Engines.Item("T-SQL") 
     dbgeng(2) = trans.Engines.Item("Managed") 

     For Each theProcess As EnvDTE80.Process2 In dbg2.GetProcesses(trans, "COMPUTERNAME") 
      If theProcess.Name.Contains("w3wp.exe") Then 
       theProcess.Attach2(dbgeng) 
      End If 

     Next 

    Catch ex As System.Exception 
     MsgBox(ex.Message) 
    End Try 

End Sub 
+0

進一步,並將其分配給工具欄...滾動到底部這篇文章:http://www.milkcarton.com/blog/2007/05/20/Using+Visual+Studio+Macros+To+Increase+Productivity.aspx – 2009-12-04 17:12:01

0

你可能想看看gflags.exe。它的一個選項是一個調試器,以運行附加到每個特定可執行文件的調用。

+0

我不確定這會工作... 這是怎麼回事?每次運行w3wp.exe時都打開一個Visual Studio的新實例? 我需要幾個w3wp.exe進程連接到相同的VS實例,我需要它是我的項目加載的實例... – 2009-05-05 17:48:07

1

我知道您正在爲此任務尋找宏,並且我擁有類似的宏。不過,我想解釋一下在開始調試時將調試器附加到解決方案中的項目的方法。

這是一個鮮爲人知的功能 - 如果您在解決方案瀏覽器中右鍵單擊解決方案文件,選擇屬性,然後可以定義多個啓動項目及其操作。運行時,您的調試器將附加到列出的項目。

注意:如果您有網絡服務,它將打開一個瀏覽器窗口,但是您可以通過禁止它打開窗口來禁用該項目的屬性。

1

這是我如何附加到遠程w3wp進程。它運行速度比DanC的解決方案快一些,並且有一些額外的錯誤處理。

Private Sub AttachToW3wp(ByVal machineName As String) 
    ' In order for this to work, you have to be running the Visual Studio 2010 Remote Debugging Monitor 
    ' as your (domain) user. 
    ' It won't work if the remote debugger is running as a service. I've tried every permutation of 
    ' domain and username in the the transport qualifier, tried the obvious local system username, 
    ' even tried looking at the network traffic 
    ' in WireShark, I can't figure it out how to make it work if you are running as a service. 
    ' If you are running the debugger as a service, even running the macro that gets created by VS's 
    ' macro recorder when you attach to a process doesn't work. 
    Dim transportQualifier = machineName 
    Try 
     Dim processToAttachTo As String = "w3wp.exe" 
     Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger 
     Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default") 
     Dim dbgeng(2) As EnvDTE80.Engine 
     dbgeng(0) = trans.Engines.Item("T-SQL") 
     dbgeng(1) = trans.Engines.Item("Managed (v2.0, v1.1, v1.0)") 
     Dim processesRemote = dbg2.GetProcesses(trans, transportQualifier) 
     Dim attached As Boolean = False 
     Dim processRemote As EnvDTE80.Process2 
     For Each processRemote In processesRemote 
      ' For some reason it takes a much longer time to get the remote process names then it 
      ' does the user name, so let's skip over all the processes that have the wrong UserName. 
      If processRemote.UserName = "NETWORK SERVICE" AndAlso _ 
       (Right(processRemote.Name, Len(processToAttachTo)) = processToAttachTo) Then 
       If processRemote.IsBeingDebugged Then 
        MsgBox(processToAttachTo & " on " & machineName & " is already being debugged") 
       Else 
        processRemote.Attach2(dbgeng) 
       End If 
       attached = True 
      End If 
     Next 
     If Not attached Then 
      MsgBox(processToAttachTo & " is not running on " & machineName & ".") 
     End If 
    Catch ex As System.Exception 
     MsgBox("Exception attempting to attach to " & transportQualifier & ": " & ex.Message) 
    End Try 
End Sub 
相關問題